C语言程序输入错误时重复提示问题的解决咨询
问题分析与解决方案
你的问题根源出在输入缓冲区的残留字符上:当用户输入错误字符(比如x)并按下回车时,输入缓冲区里实际存储的是x和换行符\n。第一次调用getchar()读取了x,触发default分支显示警告和菜单;当循环再次执行时,getchar()会读取缓冲区里剩下的\n,这又会触发default分支,导致警告和菜单重复显示两次。
解决步骤
我们需要在每次读取用户输入后,清空输入缓冲区里的残留字符(尤其是换行符),避免后续的getchar()读取到无效的换行符。另外你的代码里还有一个小错误:cube()函数里多了一个多余的function关键字,需要删除。
修改后的代码
下面是修复后的关键部分和完整代码:
关键修改点
在读取用户输入的q = getchar();之后,添加一段代码清除缓冲区:
q = getchar(); // reading user input and assign it to variable q // 清除输入缓冲区中剩余的所有字符,直到遇到换行符 while ((getchar()) != '\n');
同时删除cube()函数里的function关键字:
// 错误写法: // cube = pow(h, 3); function printf("%d\t\t%d\n", h, cube); // 正确写法: cube = pow(h, 3); printf("%d\t\t%d\n", h, cube);
完整修复后的代码
#include <stdio.h> #include <math.h> #include <stdlib.h> #define ACTION "Enter a number inside the range specified above:" // Function declaration void factorialeven(); void factorialodd(); void square(); void cube(); void fifthpower(); void mainmenu(); void secondmenu(); int main() { int y = 1; char q; mainmenu(); while (y) { printf("\n"); printf("%s", ACTION); q = getchar(); // reading user input and assign it to variable q // 清除输入缓冲区中剩余的字符(包括换行符) while ((getchar()) != '\n'); switch (q) { case '1': factorialeven(); break; case '2': factorialodd(); break; case '3': square(); break; case '4': cube(); // option 4 for cube break; // End case 4 case '5': fifthpower(); // option 5 for 5th power break; // End case 5 case '6': exit(0); break; // End case 6 default: // telling the user that the input is invalid printf("\n**********You have entered unavailable option***********\n"); printf("\n*****Please Enter any one of below available options****\n "); secondmenu(); // displaying the menu without header } // End switch } // End while return 0; } // End main // Function definitions void mainmenu() // Menu of selection { printf("\n---------- Welcome to our Project 1 ---------- \n"); printf("\nPress 1 to calculate Factorials for Even Numbers between 2 and 16\n"); printf("\nPress 2 to calculate Factorials for Odd Numbers between 1 and 15\n\n"); printf("Press 3 to calculate the Square of number between -14 and 14\n\n"); printf("Press 4 to show the Cube of numbers between -14 and 14\n\n"); printf("Press 5 to show Fifth Power numbers between -12 and 12\n\n"); printf("--------- Press '6' to quit the program ---------\n"); } void secondmenu() { printf("\nPress 1 to calculate Factorials for Even Numbers between 2 and 16\n\n"); printf("\nPress 2 to calculate Factorials for Odd Numbers between 1 and 15\n\n"); printf("Press 3 to calculate the Square of number between -14 and 14\n\n"); printf("Press 4 to show the Cube of numbers between -14 and 14\n\n"); printf("Press 5 to show Fifth Power numbers between -12 and 12\n\n"); printf("--------- Press '6' to quit the program ---------\n"); } // End function int fact(int b) { if (b <= 1) { return 1; } else { return b * (fact(b - 1)); } } void factorialeven() { int t = 0; printf("\nEven numbers\tFactorials\n"); while (t <= 15) { t += 2; printf("%d\t\t%d\n", t, fact(t)); // Displays the results } // End while } // End function void factorialodd() { int r = 17; printf("\nOdd numbers\tFactorials\n"); while (r >= 2) { r -= 2; printf("%d\t\t%d\n", r, fact(r)); } } void square() { int g = 15; int square; printf("\nNumbers\t\tSquare\n"); while (g >= -13) { g -= 1; square = pow(g, 2); printf("%d\t\t%d\n", g, square); } } void cube() { int h = -15; int cube; printf("\nNumbers\t\tCube\n"); while (h <= 13) { h += 1; cube = pow(h, 3); printf("%d\t\t%d\n", h, cube); } } void fifthpower() { int l = -13; int fifthpower; printf("\nNumbers\t\tFifth Power\n"); while (l <= 11) { l += 1; // Increases the counter by 2 fifthpower = pow(l, 5); printf("%d\t\t%d\n", l, fifthpower); } }
额外建议
- 用
pow()计算整数次方可能会有精度问题(比如pow(-2,3)可能得到-7而不是-8),可以自己写整数次方的函数,或者用乘法来计算,避免浮点精度误差。 main()函数的标准返回类型是int,我已经在代码里改成了int main()并添加了return 0;,这比void main()更符合C语言标准规范。
内容的提问来源于stack exchange,提问作者guasabito
相关产品推荐
相关产品推荐

