如果用戶按Enter鍵而不輸入任何浮點值,則需要程序退出while循環。謝謝!如何檢查用戶是否在scanf中輸入空行c
printf("Enter scores\n」);
float scores[10];
int n=0;
while (n<10){
scanf("%f", &scores[n]);
n++;
if (THE USER PRESSES ENTER WITHOUT ENTERING ANYTHING){break;}
}
如果用戶按Enter鍵而不輸入任何浮點值,則需要程序退出while循環。謝謝!如何檢查用戶是否在scanf中輸入空行c
printf("Enter scores\n」);
float scores[10];
int n=0;
while (n<10){
scanf("%f", &scores[n]);
n++;
if (THE USER PRESSES ENTER WITHOUT ENTERING ANYTHING){break;}
}
我們知道,如果沒有從輸入中提取定義的類型,scanf將返回負值。
所以:
printf("Enter scores\n」);
float scores[10];
int n=0;
while (n<10){
if(scanf("%f\n", &scores[n]) < 0) printf("Error! Error!");
n++;
}
我想。你想檢查一個整數是否被分配了一個值? 實際上,您必須初始化變量,否則它將包含在該內存位置發生的任何事情,除非它被聲明爲全局變量。如果你沒有得到,然後檢查 this might help you with your question
該代碼正在讀取浮點數,而不是整數。我想你已經錯過了這個問題的要點 - 這是關於在使用scanf()函數系列時檢測何時輸入空行的問題。 –
您可以使用fgets()
通過換行符到緩衝區中讀取輸入行,然後用sscanf()
解析緩衝區的內容。使用scanf()
這個問題是,大多數轉換說明符,特別是%f
轉換說明符,跳過前導空白,包括換行符。因此,如果您嘗試給空行scanf()
,該功能將繼續等待輸入,直到您輸入非空白字符。
下面的代碼將此技術應用於您的代碼。變量n
已更改爲size_t
類型變量,因爲這是一個無符號類型,保證能夠保存任何數組索引。此外,請注意,fgets()
和sscanf()
都返回應檢查的值。如果出現錯誤,fgets()
函數將返回空指針,並且下面的代碼將打印錯誤消息並在出現此錯誤時退出。 sscanf()
函數返回成功轉換的次數,並且可以使用此值確保輸入符合預期。當用戶輸入一個空行或者一個沒有前導浮點值的行時(前導空格是OK),返回零,並且輸入循環被轉義。
我添加了一些代碼來顯示輸入到數組中的值。
#include <stdio.h>
#include <stdlib.h> // for exit()
int main(void)
{
float scores[10];
char buffer[100];
size_t n = 0;
printf("Enter scores\n");
while (n < 10){
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
fprintf(stderr, "Error in fgets()\n");
exit(EXIT_FAILURE);
}
if (sscanf(buffer, "%f", &scores[n]) == 1) {
++n;
} else {
break;
}
}
for (size_t i = 0; i < n; i++) {
printf("scores[%zu] = %f\n", i, scores[i]);
}
return 0;
}
樣品的相互作用:從解析用戶文本的
Enter scores
3.8
3.6
2.9
3.4
scores[0] = 3.800000
scores[1] = 3.600000
scores[2] = 2.900000
scores[3] = 3.400000
單獨的輸入。
閱讀用戶輸入的行作爲字符串;
char buffer[80];
if (fgets(buffer, sizeof buffer, stdin) == NULL) Handle_EOF();
現在,解析使用sscanf()
,strtod()
,字符串等
if (sscanf(buffer, "%f", &scores[n]) == 1) Handle_Successful_Float_Input(scores[n]);
else if (buffer[0] == '\n') Handle_Empty_Line();
else Handle_Everything_Else(buffer);
@DavidBowling有時OP像簡潔的答案,有時冗長。它是一款投幣式遊戲,適合人們使用。 – chux
fgets
是更好的路線,但如果必須使用的scanf,換行可以通過掃描一個字符來檢測。如果該字符不是換行符,則用ungetc
將其替換爲輸入,然後掃描該浮點數。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
int main (void) {
char i = '\0';
int n = 0;
int result = 0;
float scores[SIZE] = { 0.0f};
printf ("enter scores\n");
while (n < SIZE && (scanf("%c",&i) == 1)) {//scan a character
if (i == '\n') {
break;//newline so leave loop
}
else {
ungetc (i, stdin);//replace the character in input stream
}
if ((result = scanf ("%f", &scores[n])) == 1) {//scan a float
printf (" score[%d] entered as %f\n", n, scores[n]);
n++;
}
if (result == 0) {
printf ("please enter a number\n");//could not scan the input as a float
}
while ((result = getchar ()) != '\n') {//clean the input stream
if (result == EOF) {
fprintf (stderr, "problem EOF\n");
return 1;
}
}
printf ("enter score[%d]\n", n);
}
return 0;
}
當你開始編寫真實的東西時,你必須避免使用'scanf()'。 –
我會嘗試獲取() – mosh442
@ mosh442:絕對不是'gets()'! SO是一個健康的生活場所,使用'gets()'就像它不健康一樣。現在,'fgets()'完全是另外一回事了。 –