据我所知,无论有任何其他问题,下面的程序都应该打印出标题和菜单选项,然后提示用户输入。
但是,它绝对什么都不做,当我停止执行时,它会打印出菜单等,然后,由于它没有要求用户输入选项,它会重复打印“这不是一个有效的选项”行。
*编辑:我已经完全删除了循环。我在程序中所拥有的只是打印标题、打印菜单、要求用户输入,而在我终止之前,我仍然什么也得不到控制台。我要求输入有什么问题吗?
EDIT2:这绝对是 scanf ,因为没有它一切正常。我运行带有附加功能的代码以打印出存储在选项中的值,它告诉我 -1 当我之前没有将其设置为 0 之前要求用户输入。该程序似乎在自动分配选项,而不是费心询问用户他们想要什么。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main ()
{
/*Print Title*/
printf("Maths Quiz Game \n");
printf("\n");
int i;
int rightCount = 0; //count of right answers
int wrongCount = 0; //count of wrong answers
int questions = 0; //user input for number of questions
int exit = 0; //store exit option
int option = 0; //menu option
while(exit == 0) //while loop that keeps program running until exit is chosen
{
/*Menu Options*/
printf("Please choose an option from the menu below. Enter the number of your choice. \n");
printf(" 1. Choose number of questions for this round. (Max = 5) \n");
printf(" 2. Start Quiz \n");
printf(" 3. Display total of right and wrong answers. (Only availanle after quiz) \n");
printf(" 4. Exit Game \n");
scanf("%d", &option); //taking user menu option
/*Error check for any input that is not a valid option. It continues until valid entry*/
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
scanf("%d", &option);
}