Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
i have to run a c program in linux but there's a getche() which i used in c program. how can i get rid of this getche() in order to run the c code in Linux.
{
/*some code*/
choice=getche();
}while((choice != 'q'));

yea i have tried that but its not giving the output which i wanted....This is the actual code ...can u plz run it in C on windows...i want the exact output in Linux as well...the code is as follows:-
/*This program deals with CAI (Computer Aided Instruction) which is often used to both develop and consolidate skills. This programming exercise involves the presentation of arithmetic questions to the user and provides varied feedback. The user h
ave the option to choose a range of numbers to be used in the calculations; for example: single digit numbers or numbers up to 20.*/
/*Author: Sukhminder Singh*/
/*Ver: cp728-1.0*/
/*Student ID: 2450362*/
#include<stdio.h>void getGrade(char);
main()
{
/*declarations*/
int i,x,j,k,min,max,option,grade=0,total=0,totalques,range,true=0,false=0,reply,reply1;
int choice1;
char choice;
clrscr();
printf("******************************************************************************");
printf("\n******************************************************************************");
printf("\n======================CHECK YOUR MATHEMATICAL SKILLS==========================");
printf("\n******************************************************************************");
printf("\n******************************************************************************");
printf("\n\nPLEASE ENTER A RANGE OF NUMBERS TO BE USED IN CALCULATIONS:");/*prompting the user for Range*/
printf("\nPlease Enter from -50 to +50:\n");
min=-51;
do/*First do while*/
{
do
{
printf("From What Number:");scanf("%d",&min);/*inputting minimum range*/
}while(min<-50);/*end of min do-while*/
max=51;
do
{
printf("Till What Number Please...");scanf("%d",&max);/*inputting maximum range*/
}while(max>50);/*end of max do-while*/
}while(max<=min);/*end of First do-while*/
range=max-min+1;
do/*main do-while loop*/
{x=rand() %10;/*using random function to randomise the switch case choices*/
clrscr();
switch (x)/*start of switch case*/
{
case 0:
case 1:
case 2:
case 3: j=random(range)+min;/*calculating the random value for inputing to the question*/
k=random(range)+min;
printf("\n\tWhat is %d times %d ? ",j,k);
scanf("%d",&reply);/*asking for the user reply*/
if(reply == (j*k))/*comparing the value of reply from user with the actual answer*/
{
true++;/*incrementing the value if true*/
getGrade('t');/*calling function for Display Prompts*/
}
else
{
false++;/*incremeting the variable if user reply is false*/
getGrade('f');/*calling the function for Display Prompts*/
}
total++;/*keeping record of the total questions asked*/
break;/*breaking out of the case*/
case 4:
case 5:
case 6: j=random(range)+min;
k=random(range)+min;
printf("\n\tWhat is %d plus %d ? ",j,k);
scanf("%d",&reply);
if(reply == (j+k))
{
true++;
getGrade('t');
}
else
{
false++;
getGrade('f');
}
total++;
break;
case 7:
case 8:
case 9: j=random(range)+min;
k=random(range)+min;
printf("\n\tWhat is %d minus %d ? ",j,k);
scanf("%d",&reply);
if(reply == (j-k))
{
true++;
getGrade('t');
}
else
{
false++;
getGrade('f');
}
total++;
break;
case 10:
case 11:
case 12: j=random(range)+min;
k=random(range)+min;
printf("\n\tWhat is %d divided by %d ?",j,k);
scanf("%d",&reply);
if(reply ==(j/k))
{
true++;
getGrade('t');
}
else
{
false++;
getGrade('f');
}
total++;
break;
}/*end of switch-case*/
printf("\n\t\tThe Number of Correct Answers so far is %d",true);/*Displaying the no of True Answers*/
printf("\n\t\tThe Number of Incorrect Answers so far is %d",false);/*Displaying the no of Flase Answers*/
printf("\n\t\tTotal questions asked %d",total);/*Displaying the number of Total Questions asked*/
printf("\n\nPress q to quit or any other key to continue....\n");
choice=getche();}while((choice != 'q'));/*end of first do-while*/
grade = ((float)true / total * 100);/*Calculating the Grade according to the Correct Answers from Total*/
if(grade>=80)/*Start of If Condition*/
printf("\n\t\tYour Grade is: A");
else if(grade>=70)
printf("\n\t\tYour Grade is: B");
else if(grade>=60)
printf("\n\t\tYour Grade is: C");
else if(grade>=50)
printf("\n\t\tYour Grade is: D");
else if(grade>=40)
printf("\n\t\tYour Grade is: E");
else/*end of If Condition*/
printf("\n\t\tYour grade is Below 39");
getch();
return 0;/*successful comletion of program statements*/
}
void getGrade(char ch)/*Function to Display the Prompts if user reply is true or false*/
{
int res;
res=rand()%4+1;
if(ch=='t')/*Checking if the user reply is true*/
{
switch(res)/*start of switch statement*/
{
case 1: printf("\n\nExcellent!!!");
break;
case 2: printf("\n\nVery Good!!!");
break;
case 3: printf("\n\nNice Work!!!");
break;
case 4: printf("\n\nKeep Up the Good Work!!");
break;}/*end of switch case*/
}
else/*if the user reply is false*/
{
switch(res)/*start of switch case*/
{
case 1: printf("\n\nNo, Please Try Again!!");
break;
case 2: printf("\n\nWrong. Try Once More!!");
break;
case 3: printf("\n\nDon't Give Up!!");
break;
case 4: printf("\n\nNo. Keep Trying!!");
break;
}/*end of switch case*/
}/*end of if condition*/
}/*end of function*/

I don't have Windows so I'm afraid I can't run it. It looks like you'll need to replace the clrscr()s with system("clear")s, probably modify the random() calls and look at the Windows documentation for getche() and getch() so that you can replace them with equivalents from glibc. It can't be that difficult.

![]() |
Installing 9.0
|
Restoring old partition a...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |