i want to go back to main menu while hitting escape key in my programme.
for that i have make a function.

void exit (char[])
{
if(char==27}
{
exit(0);
}
}

but i have many user inputs which are of type int,float,char.....

so i have tried,

while(kbhit!=27)
{
//coding
}

but after pressing ESC key nothing is done

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

4
Contributors
25
Replies
27
Views
5 Years
Discussion Span

neither scanf() nor getchar() will "see"
any input from the 'ESC' key. It doesn't generate a 'character'
from the perspective of C's i/o library.

Edited by zeroliken: n/a

ok bt 27 is its ascii value.

ok bt 27 is its ascii value.

I know ...

Edited by zeroliken: n/a

okay and kbhits works for only getche and getch.So what can i use if i read a varialble by scanf or by other ways???

okay and kbhits works for only getche and getch.So what can i use if i read a varialble by scanf or by other ways???

How about any keys in the alphabet instead of using getch() or the ESC key

yes that will also work. but in my programme i am taking integer and float type of data,lots of time so instead for check ach and every input we take ,i wrote entire code in
while(kbhit!=47)
{
//coding

}block 47 is '/'(forward slash)
for more detail i'll post my coding too.

Sure I would understand you better if you post your whole code

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define SIZE 10

void FalsePosition(float[]);
void printArray1D(float [SIZE], int , char);
void title(char []);
void main()
{
int i,j,k,n,n1,obs;
float X[SIZE],Y[SIZE],dUx[SIZE][SIZE],h,sum,s,x,u;
char ch,ch1;

clrscr();

do
{ 	clrscr();
printf("\n\t\t 2. False Position Method : \n");
printf("\n\t\t 5. Exit : \n");
flushall();

switch(ch1)
{

case '2':
title("False-Position Method");
FalsePosition(X);
break;
case '5':
printf("\nHit a key to terminate the programme...");
getch();
exit(0);

default:
printf("\n Invalid choice");
break;
}

}while(ch1=='y' || ch1=='Y');
}

void CheckObservation(int n)
{
if(n<=0)
{
printf("\n\t\t Value of observation is too low...");
getch();
exit(0);
}
}

{
char ch='\0';
scanf("%c",&ch);
return ch;
}

void title(char title[])
{
printf("\t\t\t\t ");
puts(title);
}

{
int i;
printf("\n\n Enter value for the following: \n");
for(i=0;i<n;i++)
{
printf("\n%c[%d] : ",c,i);
scanf("%f",&X[i]);
}
}

// For printing 1-Dimensional array

void printArray1D(float X[],int n,char c)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
{
printf("%c[%d] : %f",c,i,X[i]);
}
}

//To find the factorial

double fact(int no)
{
int i;
float f=1;
for(i=no;i>=1;i--)
{
f*=i;
}
return(f);
}

// To calculate value of a polynomial [f(x)] for given x.
//if 		f(x)=2x^3  ,where x=1
//then   f(1)=2(1)^3   = 8

float f(float X[],int n,float x)
{
float sum=0;
int i;

for(i=0;i<=n;i++)
{
sum = sum + X[i] * pow(x,i);
}
return(sum);
}

//"False position"Or Regular-falsi position method.
// This is the Oldest Method to find the root of the polynomial..
//	x3 = ( (x1*f2) - (x2*f1) ) / ( f2-f1 );

void FalsePosition(float X[SIZE])
{

float x1,x2,epsilon=0.0001,x3=0,x3p,delta=0.001,f1,f2,f3;
int n,i=0;
char ch;

clrscr();

title("False-Position Method");
printf("\n False position is the Oldest Method to find the root of the polynomial");
printf("\n Two approximation are required.(x1 and x2)");
getch();
clrscr();
title("False-Position Method");

while((ch=getch())!=27)
{
if (ch == '@') break; //Just so that we have a known exit.
printf("\n\nEnter the degree of polynomial:");
scanf("%d",&n);
CheckObservation(n);

printf("\nEnter first point of the search interval:");
scanf("%f",&x1);
printf("\nEnter second point of the search interval:");
scanf("%f",&x2);

f1 = f( X,n,x1 );
f2 = f( X,n,x2 );
if( f(X,n,x1) == 0 )
{
printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x1);

getch();
return ;
}

if( f(X,n,x2) == 0 )
{
printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x2);

getch();
return ;
}
if(f1*f2 > 0)
{
printf("\nInitial approximation are unsuitable \n");

getch();
return ;
}

clrscr();
printf("\n \t\t\t   FALSE POSITION METHOD\n");
printf("\n i\t\t   x1\t \t   x2\t\t x3");

do
{
i++;

if(fabs(f2-f1)<=delta)
{
printf("\nSlope of funtion becomes too small \n");
printf("\nEntered roots(x1,x2) are improper..!!");
printf("\nEnter any key to continue... ");
getch();
break;
}
x3p = x3;
x3 = ( x1*f2-x2*f1 ) / ( f2-f1 );
f3 = f( X,n,x3 );

if( f1*f3 < 0 )
{
x2 = x3;
f2 = f3;
}
else
{
x1 = x3;
f1 = f3;
}

printf ("%d\t|\t%5.2f\t|\t%5.2f\t|\t%5.2f\t\n",i,x1,x2,x3);

} while( fabs( (double) ( (x3-x3p) / x3 ) ) > epsilon );

printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x3);

}
}

instead of 27 i have tried with 47 ,too.

at the while statement starting at line 151 is there a line/statement that changes the value of ch?

Edited by zeroliken: n/a

//while((ch=getch())!=27)
gets the characterin ch and chek its value but i know this will work only once and
not for all the inputs

//while((ch=getch())!=27)
gets the characterin ch and chek its value but i know this will work only once and
not for all the inputs

What I mean to say is that if there is no statement inside the while loop that changes the value of ch or a break statement the program will run continuously

while((ch=getch())!=27)
{
if (ch == '@') break; //Just so that we have a known exit.

If ch has a value different from 27 and @ and there is no other statement that changes its value then it will run continuously

why not use a boolean like statement that asks the user if he wants to continue?
pseudo:

int ans = 1;
//ask user if he wants to continue..presses 1 if yes 0 otherwise
if(userinput==0){
ans=0;
}
}

Edited by zeroliken: n/a

What I mean to say is that if there is no statement inside the while loop that changes the value of ch or a break statement the program will run continuously

while((ch=getch())!=27)
{
if (ch == '@') break; //Just so that we have a known exit.

If ch has a value different from 27 and @ and there is no other statement that changes its value then it will run continuously

why not use a boolean like statement that asks the user if he wants to continue?
pseudo:

int ans = 1;
//ask user if he wants to continue..presses 1 if yes 0 otherwise
if(userinput==0){
ans=0;
}
}

whoops change answer to ans at line 2 at the while statement :)

that is okay....And i know it by asking to user.

But the question is at "any time"
soif user wants to continue and press 'y'(yes).but after 'y' he wants to quit without going to main menu or inserting garbage values to go out of th programme.

It's like alt+X to exit from turbo c.....

Morover how can i use getch()/getche(). I am taking dealing with int & float datatype.

Yes i can convert the datatype.But then i don't know,
how to convert char '3' to int '3' OR int a to char 'a'

#include <stdio.h>
#include<conio.h>
#include<ctype.h>  /to ascii
void main()
{
char c1,*c=&c1;
int n,n1;
clrscr();
printf("\n Enter n.");
scanf("%d",&n);
c1=(char)n;       // int a to char 'a'
n1=toascii(c1) ;   // ascii of char'a'
printf("%d %c",c1,c1);
printf("\n %d %c",atoi(&c),atoi(&c));
printf("\n %d %c",n1,n1);
getch();
}

Yes i can convert the datatype.But then i don't know,
how to convert char '3' to int '3' OR int a to char 'a'

atoi

and include this library

#include <stdlib.h>

convert char to int

Edited by zeroliken: n/a

Take digits one by one till null and subtract '0', multiply by 10 and add to convert char to int. For int to char, take %10 and add to character variable with value '0' till all digits, then reverse string

@zeroloken : atoi results 0 each time.

@v3ga : Thanks....But Please, simplify.....its a bouncer for me

Edited by dev90: n/a

In order to convert a single character to integer, u have to subtract the ascii value of '0' from it since the characters 0,1,2.. take continuous ascii values. Put that in a loop to extract digits one by one and then the integer can be made.

int inum=0;
char cnum[15];
gets(cnum);
for(int i=0;cnum[i]!='\0';i++)
{
inum=inum*10+cnum[i]-'0';
}

Here cnum is the number as string and inum is the integer

Similarly to convert integer to character, find reminder on div by 10 to extract the last digit, add to character variable initialised to '0' and catenate to a string. But since the digits are extracted from the end, the reverse string will be formed. So u need to reverse the string in the end

what if i write 'a' as integer.will it be same???
Can you display the code ,please.

Thank you. i learn a new method.

But I want Either (1) scanf("%d",&n); ////where n is A
And convert it to char 'A'
OR (2) ch=getch();//n='3' convert it to 3

But I think I can try it by modifying your method....thanks,again :)

when i said integer to character i meant the numbers. eg:converting 1947 to "1947" . int cannot store value a.

int num,i;
scanf("%d",&inum);
cnum[15]="000000000000000";
for(i=0;inum>0;i++,inum/=10)
cnum[i]=cnum[i]+inum%10;
cnum[i]='\0';
strrev(cnum);

Sorry if i changed the topic u got how to exit anytime?

Edited by v3ga: forgot to reverse

v3Ga:

Actually i am dealing with numbers as i said above.
So, exit from anyWhere will will work if input char from getch()/getche()
n use
while(kbhit()!=27) //for escape key
{
.....
}
This will work.
And this will not work in my project anymore.

Even though u have tried that its great, but yet not the same! The solution is simple.
When using getch(), when u press any key its ASCII Value is saved.
Also , u are correct the Ascii value for the esc button is 27. U can just do the following :

if(getch()==27)
main();

OR if u also want to store the key input

char a;
a=getch();

if(a==27)
main();