Dev C++下C语言MCQ测试程序输入异常问题求助
问题描述
编程新手使用Dev C++编写C语言选择题测试程序时遇到输入异常:第一个题目可正常接收输入,第二个题目无法获取输入直接判定错误。
程序代码
#include<stdio.h> #include<conio.h> #include<string.h> #include<math.h> #include <stdlib.h> #include<windows.h> #include <time.h> void main() { printf("Welcome to MCQs Test.\nPhysics CH # 1\nExercise MCQs......\nChoose the correct answer.\n\n"); char ans; int true = 0, false = 0, count = 0; printf("1) Which of the following is an example of Simple Harmonic Motion?\n\n"); printf("A. Motion of a simple pendulum\nB. The motion of ceiling fan\nC. The spinning of the Earth around its axis\nD. A bouncing ball on a floor\n\n"); printf("Select your answer (a, b, c, d): "); scanf("%c", &ans); if(ans == 'a'){ printf("\nCorrect ^_^\n\n"); true++; } else if(ans != 'a'){ printf("\nOops! Your answer is Wrong.\nThe Correct answer is: 'A. Motion of a simple pendulum' \nBetter luck Next time....\n\n"); false++; } count++; printf("Press any key to continue...\n\n"); getch(); printf("2) If the mass of the bob of a pendulum is increased by a factor of 3, the period of the pendulum`s motion will\n\n"); printf("A. be increased by a factor of 2\nB. remain the same\nC. be decreased by a factor of 2\nD. be decreased by a factor of 4\n\n"); printf("Select your answer (a, b, c, d): "); scanf("%c", &ans); if(ans == 'b'){ printf("\nCorrect ^_^\n\n"); true++; } else if(ans != 'b'){ printf("\nOops! Your answer is Wrong.\nThe Correct answer is: 'B. remain the same' \nBetter luck Next time....\n\n"); false++; } count++; printf("Press any key to continue...\n\n"); getch(); printf("%d of your answers is/are correct while %d is/are incorrect out of %d Questions\n", true, false, count); printf("Press any key to continue...\n\n"); getch(); }
程序运行输出
Welcome to MCQs Test. Physics CH # 1 Exercise MCQs...... Choose the correct answer. 1) Which of the following is an example of Simple Harmonic Motion? A. Motion of a simple pendulum B. The motion of ceiling fan C. The spinning of the Earth around its axis D. A bouncing ball on a floor Select your answer (a, b, c, d): a Correct ^_^ Press any key to continue... 2) If the mass of the bob of a pendulum is increased by a factor of 3, the period of the pendulum`s motion will A. be increased by a factor of 2 B. remain the same C. be decreased by a factor of 2 D. be decreased by a factor of 4 Select your answer (a, b, c, d):
实际异常表现
Oops! Your answer is Wrong. The Correct answer is: 'B. remain the same' Better luck Next time.... Press any key to continue... 1 of your answers is/are correct while 1 is/are incorrect out of 2 Questions Press any key to continue...
问题原因与解决方法
原因分析
第一个题目输入完成后,按下的**回车键(换行符\n)**会残留在输入缓冲区中。当第二个scanf("%c", &ans);执行时,它会直接读取这个换行符,而非等待用户输入新字符,导致程序直接判定答案错误。
解决方法
有两种简单的修复方式:
方法1:在scanf格式字符串前添加空格
将所有scanf("%c", &ans);修改为scanf(" %c", &ans);。这里的空格会跳过输入缓冲区中的所有空白字符(包括换行符、空格、制表符等),确保scanf读取到的是用户输入的有效字符。
修改后的关键代码片段:
printf("Select your answer (a, b, c, d): "); scanf(" %c", &ans); // 格式字符串前添加空格
方法2:手动丢弃缓冲区中的换行符
在第一个题目处理完成后的getch();之后,添加getchar();来读取并丢弃缓冲区中残留的换行符:
printf("Press any key to continue...\n\n"); getch(); getchar(); // 丢弃缓冲区中的换行符
额外建议
- 标准C中
main函数的返回类型应为int,而非void main(),建议修改为int main()并在函数末尾添加return 0;。 true和false是C99及以后标准的关键字,避免将它们用作变量名,可改为correct、incorrect等名称,避免潜在编译问题。
内容的提问来源于stack exchange,提问作者Huzaifa Tahir
相关产品推荐
相关产品推荐

