954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Exit from programme anytime

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

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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.

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

ok bt 27 is its ascii value.

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
ok bt 27 is its ascii value.


I know ...
check this link for more info ESC key in c

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

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???

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
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

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

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.
Please help n thank you zeroliken....

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define SIZE 10

void FalsePosition(float[]);
char ask();
void readArray1D(float [SIZE],int,char);
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();
		ch1=ask();


		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 ask()
{
	char ch='\0';
	printf("\n\nEnter your choice :");
	scanf("%c",&ch);
	return ch;
}

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

// For reading 1-Dimensional array.

void readArray1D(float X[],int n,char c)
{
	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);
		readArray1D(X,n+1,'X');

		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);

	}
}
dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

instead of 27 i have tried with 47 ,too.

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

//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

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
//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;
while(answer==1){
      //ask user if he wants to continue..presses 1 if yes 0 otherwise
      if(userinput==0){
         ans=0;
         }
      }
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

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;
while(answer==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 :)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

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.....

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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'

Please, Help me in this.

#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();
}
dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 
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'


Read this atoi

and include this library

#include <stdlib.h>


then read this convert char to int

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

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

v3ga
Junior Poster in Training
95 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

@zeroloken : atoi results 0 each time.

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

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

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

v3ga
Junior Poster in Training
95 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: