C程序输出有问题

时间:2017-11-11 18:48:45

标签: c compiler-errors output borland-c

据我所知,无论其他问题如何,下面的程序都应打印标题和菜单选项,然后提示用户输入。

然而,它绝对没有什么,当我停止执行它打印出菜单等然后,因为它没有要求用户输入选项它重复打印“这不是一个有效的选项”行。 / p>

*编辑:我已经完全删除了循环。程序中的所有内容都是打印标题,打印菜单,询问用户输入,直到我终止后,我仍然没有得到任何信息。我要求输入有什么问题吗?

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

3 个答案:

答案 0 :(得分:2)

如何改变

while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
    printf("\n That is not a valid option. Please try again. \n");
    scanf("%d", &option);
}

之类的东西
if ((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
    printf("\n That is not a valid option. Please try again. \n");
    // scanf("%d", &option);  // This is probably not required
}

if ( option >= 1 && option <= 4)
{
    printf("\n That is not a valid option. Please try again. \n");
    // scanf("%d", &option);
}

因为你在外面使用无限循环,为什么你需要一个内部?如果选择了不可用的选项,您只需选中该选项并显示菜单。

您之后可以在此if中添加所有逻辑,每个选项都有一个。

更好地使用switch以获得良好的理解

/* After selecting an option */
switch (option)
{
    case 1:
         /* Do the operation according */
         break;

    case 2:
         /* Do the operation according */
         break;

    case 3:
         /* Do the operation according */
         break;

    case 4:
         /* Do the operation according */
         break;

    default:
         /* If none of the option selected */
         printf ("Wrong input! \n")
         break;
}

希望你明白了。

答案 1 :(得分:1)

while((option != 1) || (option != 2) || (option != 3) || (option != 4))

您输入的选项值假设为1,while()的第一个条件为false但剩余为true,因此进入循环并打印“这不是有效选项。请再试一次。”所以替换||与逻辑和(&amp;&amp;)

while((option != 1) && (option != 2) && (option != 3) && (option != 4))

现在如果您输入了正确的输入,则不会显示“这不是有效选项。请再试一次”

答案 2 :(得分:0)

问题在于printf功能,因为它在您输入以下选项之后才打印出来,直到用户回答之后才会询问。 printf对此进行了简单的刷新。

相关问题