我想制作一個在用戶按下之前一直運行的程式ESC。而且我需要處理用戶的輸入,因為我輸入了一個應該是整數的值。我怎樣才能做到這一點?
#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;
}
程式應該計算給定數字的階乘。
我試過這個,但它使一個無限回圈。
if(scanf("%d", &sayi) != 1){
printf("Error Occured.\n");
continue;
}
uj5u.com熱心網友回復:
在 Microsoft Windows 上,無法ESC使用 C 標準庫提供的函式檢測密鑰。
但是,可以使用以下特定于平臺的功能來執行此操作:
_getch_getcheReadConsoleInput
不過,使用這些功能時,您必須自己撰寫基本輸入功能。這包括
- 決定何時將輸入回顯給用戶和
- 當用戶按下退格鍵時洗掉最后一個字符。
因此,如果您正在尋找一個簡單的解決方案,這可能不是您想要的。繼續使用 C 標準庫提供的函式并讓用戶做一些其他事情來表明他們已經完成會更容易,例如-1在這種情況下告訴用戶輸入數字。
如果您想要一些更優雅的東西,例如讓用戶輸入q或quit代替-1他們完成時,那么您可以使用函式fgets代替scanf,以便首先從用戶讀取一行輸入作為字串。然后,您可以將此字串與qor進行比較quit,并且僅當此比較失敗時,您才嘗試將字串轉換為數字,例如使用函式strtol。
這是一個例子:
#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';
}
}
該程式具有以下行為:
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...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/530825.html
標籤:C视窗循环用户输入计算器
