Fgets()要求在读取char数组后输入两次(Fgets() requires enter to be hit twice after reading to char array)

编程入门 行业动态 更新时间:2024-10-19 19:23:54
Fgets()要求在读取char数组后输入两次(Fgets() requires enter to be hit twice after reading to char array)

当我在函数isFloat(char array[])输入任何评估为false的内容时 ,我需要按两次Enter键以保持程序运行。

如果我注释掉除fget()命令之外的所有内容,一切都要求我输入两次。 可能是什么导致了这个? 我正在正确刷新stdin并且\n被strtok()删除。 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; }

When I enter anything that evaluates to be false in function isFloat(char array[]) I need to hit enter twice to keep the program running.

If I comment out everything but the fget() command everything requires me to hit enter twice. What could be causing this? I'm flushing stdin properly and the \n is removed by strtok(). Is the printf() function causing problems? I've read that scanf() and fgets() can cause problems when used together. But here they arent.

Problem Area

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

Full Code:

#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; }

最满意答案

当我在函数isFloat(char array[])输入任何评估为false的内容时,我需要按两次Enter键以保持程序运行。

那是因为你有一行代码要求你输入一行文字。

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

When I enter anything that evaluates to be false in function isFloat(char array[]) I need to hit enter twice to keep the program running.

That's because you have a line of code that expects you to enter a line of text.

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

更多推荐

本文发布于:2023-04-28 07:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   数组   Fgets   char   requires

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!