I want to make a program that works endlessly until the user presses ESC. And I need to handle the user's input, because I input a value which should be an integer. How can I make this?
#include <stdio.h>
int main(){
int num,factorial;
while(1){
factorial = 1;
printf("Enter Number : ");
scanf("%d",&num);
for(int i = 1 ; i<=num ; i ){
factorial *= i;
}
printf("Factorial : %d\n",factorial);
}
return 0;
}
Program should calculate factorial of a given number.
I tried this, but it makes an endless loop.
if(scanf("%d", &sayi) != 1){
printf("Error Occured.\n");
continue;
}
CodePudding user response:
On Microsoft Windows, it is not possible to detect the ESC key using the functions provided by the C standard library.
However, it is possible to do this with the following platform-specific functions:
When using these functions, you will have to program basic input functionality yourself, though. This includes
- deciding when to echo the input back to the user and
- deleting the last character when the user presses the backspace key.
Therefore, if you are looking for a simple solution, this is probably not what you want. It would be easier to continue using the functions provided by the C standard library and to make the user do something else to indicate that they are finished, for example to tell the user to enter the number -1
in this case.
If you want something slightly more elegant, for example to make the user enter q
or quit
instead of -1
when they are finished, then you could use the function fgets
instead of scanf
, in order to first read one line of input from the user as a string. You can then compare this string with q
or quit
, and only if this comparison fails do you attempt to convert the string to a number, for example using the function strtol
.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//forward declaration of function
void get_line_from_user( char *buffer, int buffer_size );
int main()
{
while ( 1 )
{
char line[200], *p;
long num, factorial = 1;
//prompt user for input
printf("Enter number, or \"q\" to quit: ");
//read one line of input from user
get_line_from_user( line, sizeof line );
//determine whether user wants to quit
if ( line[0] == 'q' )
{
printf( "Quitting program...\n" );
exit( EXIT_SUCCESS );
}
//attempt to convert input string to a number
num = strtol( line, &p, 10 );
//verify that conversion was successful
if ( p == line )
{
printf( "Input error!\n" );
continue;
}
//perform calculations
for( int i = 2; i <= num; i )
{
factorial *= i;
}
//print the result
printf( "Factorial : %ld\n", factorial );
}
return 0;
}
//This function will read exactly one line of input from the
//user. On failure, the function will never return, but will
//print an error message and call "exit" instead.
void get_line_from_user( char *buffer, int buffer_size )
{
char *p;
//attempt to read one line of input
if ( fgets( buffer, buffer_size, stdin ) == NULL )
{
printf( "Error reading from input\n" );
exit( EXIT_FAILURE );
}
//attempt to find newline character
p = strchr( buffer, '\n' );
//make sure that entire line was read in (i.e. that
//the buffer was not too small to store the entire line)
if ( p == NULL )
{
//a missing newline character is ok if the next
//character is a newline character or if we have
//reached end-of-file (for example if the input is
//being piped from a file or if the user enters
//end-of-file in the terminal itself)
if ( getchar() != '\n' && !feof(stdin) )
{
printf( "Line input was too long!\n" );
exit( EXIT_FAILURE );
}
}
else
{
//remove newline character by overwriting it with
//null character
*p = '\0';
}
}
This program has the following behavior:
Enter number, or "q" to quit: 5
Factorial : 120
Enter number, or "q" to quit: 3
Factorial : 6
Enter number, or "q" to quit: Test
Input error!
Enter number, or "q" to quit: 7
Factorial : 5040
Enter number, or "q" to quit: 5
Factorial : 120
Enter number, or "q" to quit: 6
Factorial : 720
Enter number, or "q" to quit: q
Quitting program...