2016-04-08 57 views
1

當我輸入任何東西評估爲假在函數isFloat(char array[])我需要點擊輸入兩次,以保持程序運行。Fgets()需要輸入在讀到字符數組後輸入兩次

如果我註釋掉一切,但fget()命令一切要求我輸入兩次。什麼可能導致這個?我正在沖洗stdin\nstrtok()刪除。 printf()函數是否會導致問題?我讀過scanf()fgets()一起使用會導致問題。但在這裏他們沒有。

問題區域

printf("first number: "); 
    fgets(input, TEN_THOUSAND, stdin); 
    strtok(input, "\n"); 
    success = isFloat(input); 
    if(success) 
     firstNum = atof(input); 

全碼

#include<stdio.h> 
#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

int isFloat(char array[]) 
{ 

int m = 0; 
int periodCount = 0; 

for(m=0; array[m] != '\000'; m++) 
    { 
     if(array[m] == '1' || array[m] == '2' || array[m] == '3' || array[m] == '4' || array[m] == '5' || array[m] == '6' || array[m] == '7' || array[m] == '8' || array[m] == '9' || array[m] == '0') 
      { 
      } 
     else 
      { 
       if(array[m] == '.' && periodCount == 0 && m != 0 && m+1 != '\n') 
        periodCount = 1; 
       else 
        return 0; 
      } 
    } 
return 1; 
} 

void eatLine() 
{ 
    while (getchar() != '\n'); 
} 

int main() 
{ 

double firstNum = 0.0; 
double secondNum = 0.0; 
double totalNum = 0.0; 
int success = 0; 
int TEN_THOUSAND = 10000; 
char input[TEN_THOUSAND]; 

//Outputs assignment header 
printf("CS201 - Lab 2 - Number Adder\n\n"); 

printf("first number: "); 
fgets(input, TEN_THOUSAND, stdin); 
strtok(input, "\n"); 
success = isFloat(input); 
if(success) 
    firstNum = atof(input); 

while(!success) 
{ 
    eatLine(); 
    //The one is for testing purposes 
    printf("-- bad input --\n"); 
    printf("first number: "); 
    fgets(input, TEN_THOUSAND, stdin); 
    strtok(input, "\n"); 
    success = isFloat(input); 
    if(success) 
     firstNum = atof(input); 
} 

printf("second number: "); 
fgets(input, TEN_THOUSAND, stdin); 
strtok(input, "\n"); 
success = isFloat(input); 
if(success) 
    secondNum = atof(input); 

while(!success) 
{ 
    eatLine(); 
    //The one is for testing purposes 
    printf("-- bad input --\n"); 
    printf("second number: "); 
    fgets(input, TEN_THOUSAND, stdin); 
    strtok(input, "\n"); 
    success = isFloat(input); 
    if(success) 
     secondNum = atof(input); 
} 

//adds the numbers 
totalNum = firstNum + secondNum; 

//Solves ugly formatting problem by firstly including a newline 
//after the input is garnered. then it outputs firstNum and totalNum 
//in a field of 11 spaces with a newline terminator. This decrements 
//11 to 10 on the secondNum line to compensate for the space that the + takes up. 
printf("\n%11.2f\n", firstNum); 
printf("%s%10.2f\n", "+", secondNum); 
printf("-----------\n"); 
printf("%11.2f\n\n", totalNum); 

return 0; 
} 
+1

問題是'eatLine'。沒有什麼可吃的,因爲'fgets'已經吃掉了整條線,包括'\ n'。所以只要刪除所有對'eatLine'的調用,它就可以工作。請注意,您可以在'scanf'中使用'eatLine',因爲'scanf'會在輸入流中留下換行符,所以總是有東西要吃。 – user3386109

+0

@ user3386109對於某些格式字符串,無論如何 –

回答

3

當我輸入任何內容,其值在功能isFloat(char array[])我需要鍵入回車兩次,以保持假程序運行。

這是因爲您有一行代碼需要輸入一行文本。

while(!success) 
{ 
    eatLine(); // Culprit 
+0

輝煌!它的工作,謝謝。因此,因爲getchar()停在'\ n',它正在等待'\ n'輸入正確的流? –

+0

@TobiasGreen,您正在使用'while(getline()!='\ n')',它讀取並丟棄所有直到幷包括換行符的所有內容。 –