[英]C Programming - Allow / determine / check “space ( )” input in integer
首先,對不起我的英語不好,好的,請轉到上面的問題。 在很多網站上,我都在網上沖浪很多有關此問題的參考,但是我沒有找到正確的答案。
我正在嘗試制作一個C程序,該程序可以確定用戶是否輸入了整數,如果用戶未輸入整數,則程序會重試提示用戶輸入整數,依此類推。 當我在條件語句上使用scanf()返回值時,一切正常,但問題是,當用戶輸入'whitespace / blankspace / space'(在ascii代碼為&#32上)並按'enter'時,我的程序將保持運行狀態等待用戶輸入一些字符或整數。
我只希望如果輸入為'whitespace / blackspace / space',程序將重復提示用戶輸入整數或程序停止。
這是案例代碼:
#include <stdio.h>
int main() {
int number, isInt;
printf("Input a number : ");
do {
if ((isInt = scanf("%d", &number)) == 0) {
printf("retry : ");
scanf("%*s");
} else if (number < 0) {
printf("retry : ");
}
} while (isInt == 0 || number < 0);
printf("%d\n", number);
return 0;
}
我是C語言的新手,對此感到很好奇。 我知道如果我對scanf()字符串使用%[^ \\ n] <-代碼格式並將其轉換為整數,我的意思是該程序將正確運行。 還有另一種使用%d代碼格式解決此問題的方法嗎? 或使用scanf()?
請幫助我打破好奇心,謹記:D
scanf
將忽略空格,並繼續查找,直到找到非空格,然后再嘗試進行轉換。
我建議使用fgets獲取輸入行,然后使用sscanf處理。 像這樣:
#include <stdio.h>
#include <stdlib.h>
int main() {
int number=-1;
printf("Input a number : ");
do {
char line[80];
if(!fgets(line,sizeof(line),stdin)){
printf("line read error\n");
exit(1);
}
if (sscanf(line,"%d", &number) !=1) {
printf("retry : ");
} else if (number < 0) {
printf("retry : ");
}
} while ( number < 0);
printf("%d\n", number);
return 0;
}
格式說明符'%* s'在遇到空格(制表符,空格,換行符)時將停止輸入字符,因此,如果用戶輸入了這些字符中的任何一個,這些字符將不會被使用。
'%d'將消耗所有此類前導空白。
當用戶輸入一些“有效”數字時,該代碼將進入“ else”語句
當用戶輸入諸如“ a”之類的ascii值時,“%d”將完成輸入,並且代碼將進入“ else”語句。 到那時,“ number”變量將不會被設置,因此“ number”將包含“ number”變量所在堆棧中碰巧的每個垃圾。 該“可能”恰好是大於0的整數。
從調用'scanf()'返回的值可以是0或1以外的值,例如EOF
“退格鍵”將由終端驅動程序占用,因此請不要訪問該程序。
因此您的程序確實可以實現預期的功能,但可能並非您想要的。
%s
將跳過任何前導空格,然后讀取非-空格字符,直到再次看到空格。 因此,該scanf( "*%s" );
調用將一直阻塞,直到看到至少一個非空白字符為止。
%[
轉換說明符不會跳過任何前導空格,因此可以作為可接受的替代方法:
scanf( "%*[^\n]" );
盡管可以說,更好的方法是將所有輸入讀取為文本,然后使用strtol
將文本轉換為目標類型,但這種方法在快速而骯臟的測試中“有效”。 這是我的意思的工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define BUF_SIZE 20
int main( void )
{
int value = -1;
char buf[BUF_SIZE];
do
{
printf( "Gimme a non-negative value: " );
if ( fgets( buf, sizeof buf, stdin ) )
{
char *newline = strchr( buf, '\n' );
if ( !newline )
{
printf( "Input too long for buffer, flushing..." );
while ( fgets( buf, sizeof buf, stdin ) && !strchr( buf, '\n' ) )
;
printf( "try again\n" );
}
else
{
*newline = 0; // Remove the newline character from the buffer
char *chk; // chk will point to the first character *not* converted
// by strtol. If that character is anything other
// than 0 or whitespace, then the input string was
// not a valid integer.
int tmp = (int) strtol( buf, &chk, 0 );
if ( *chk != 0 && !isspace( *chk ) )
{
printf( "%s is not a valid integer, try again\n", buf );
}
else if ( tmp < 0 )
{
printf( "%d is negative, try again\n", tmp );
}
else
{
value = tmp;
}
}
}
else
{
printf( "Input failure, bailing out completely\n" );
break;
}
} while ( value < 0 );
printf( "value is %d\n", value );
return 0;
}
而且,這是一個運行每個測試用例的示例:
$ ./format2
Gimme a non-negative value: Supercalifragilisticexpealidocious
Input too long for buffer, flushing...try again
Gimme a non-negative value: 123fgh
123fgh is not a valid integer, try again
Gimme a non-negative value: -12345
-12345 is negative, try again
Gimme a non-negative value: 1234
value is 1234
為了測試fgets
失敗,我輸入Ctrl - d發送EOF
:
$ ./format2
Gimme a non-negative value: Input failure, bailing out completely
value is -1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.