1

As far as I know regardless of any other issues the program below should print out the title and menu options, and then prompt for user input.

However, it does absolutely nothing and when I stop the execution it prints out the menu etc and then, as it hasn't asked for the user inputted option it repeatedly prints the "This is not a valid option" line.

*EDIT: I have completely removed the loops. All I have in the program is print the title, print the menu, ask for user input, and I still get nothing to the console until after I terminate. Is there something wrong with my asking for input?

EDIT2: It's definitely the scanf as without it everything works. I ran the code with an added function to print out the value stored in option and it told me -1 when I hadn't previously set it to 0 before asking the user to input. The program seems to be automatically assigning option instead of bothering to ask the user what they want.

#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);
        }
Amanda6276
  • 33
  • 9
  • 1
    Your code is not complete I guess – Vipin Yadav Nov 11 '17 at 18:52
  • 3
    `while((option != 1) || (option != 2) || (option != 3) || (option != 4))` -->> `while(option != 1 && option != 2 && option != 3 && option != 4)` Or: `while(option < 1 || option > 4)` – wildplasser Nov 11 '17 at 18:53
  • Hi, the code hasn't even been started yet. I was just at the stage where I had put in the invalid entry checker and said I'd give it a go. The rest of the code at the minute is just if(option=??) then print the number. – Amanda6276 Nov 11 '17 at 18:56

3 Answers3

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

what ever option value you enters assume 1, 1st condition of while() will be false but remaining are true so enters into loop and prints "That is not a valid option. Please try again." so replace || with logical And(&&).

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

now here if you entered correct input it will not display "That is not a valid option. Please try again"

Achal
  • 11,821
  • 2
  • 15
  • 37
  • While I see your point and I have changed the code, that is not the main issue here. I'm not getting any output at all. When I run the code, nothing shows on the screen at all. When I finally get sick of waiting and click stop executing, the termination screen pops up with all the output it should have given me when I ran, including multiple Not a valid option line. even after changing from or to and like you said – Amanda6276 Nov 11 '17 at 19:02
  • provide the code after while(cond1 && cond2 && cond3 && cond4), then only I can say anything. because if you enters options b/w 1 to 4, it will not print anything and it shouldn't. – Achal Nov 11 '17 at 19:09
  • if(option == 1){ printf("\n Option 1\n"); } and then the same for 2-4. I'm not sure I understand what you're saying though – Amanda6276 Nov 11 '17 at 19:11
1

How about changing

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

to something else like

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
}

or

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

Because you are using infinite loop outside, why would you need one inside? All you need is to check the option and show the menu if an unavailable option is selected.

All of your logic you can put in this if afterward, one for each option selected.


Better use switch for good understanding

/* 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;
}

Hope you got it.

Vipin Yadav
  • 1,616
  • 1
  • 14
  • 23
  • Feel free to put a comment if anything else you wanted? @Amanda6276 – Vipin Yadav Nov 11 '17 at 19:15
  • Hi, that was a really good suggestion and you've changed the output I get when I terminate the code. However, I'm still not getting anything to my output at all. If I remove the while loop completely I get the Maths Quiz line then nothing but once the loop goes in, nothing. I stare at an empty screen until i terminate, then it shows the title and menu on a loop. Do I have an issue that would cause this? – Amanda6276 Nov 11 '17 at 19:22
  • It's not printing the title line that comes before the while loop though. And I have changed the inner loop to be an if statement instead. Should the program not ask for user input as I put in under the menu options before continuing to run the while loop? – Amanda6276 Nov 11 '17 at 19:33
  • That actually depends on the requirement of your programme. If you want to be the menu only once, you can do that by putting it outside. But as if you want to continue after completing one operation for the selected option, that should be inside. All statements inside the loop will be repeated only. – Vipin Yadav Nov 11 '17 at 19:38
  • I don't know if you've seen my edit above but I was trying to figure it out on my own and I removed the whole while loop, basically making my program a print out the menu and ask for user input. It still doesn't print anything though. Do you think my input request is wrong or is there anything else it could be? Thanks for your help so far – Amanda6276 Nov 11 '17 at 19:41
  • Input request after the menu is fine. You see, before getting hands-on code first you should decide what you exactly want to do by drawing all on a paper, how the flow of your programme should be, when to ask for input and when to do an operation. Once you got the logic and algorithm, the code will be not a big deal. – Vipin Yadav Nov 11 '17 at 19:48
  • I removed the input request, ran the code and everything printed out fine. I know that its the right format and in the right spot but for some reason its not letting me do anything if its there. – Amanda6276 Nov 11 '17 at 19:54
  • Don't worry. You will figure it out. – Vipin Yadav Nov 11 '17 at 19:57
0

The problem was with the printf function in that it didn’t print out until after you had entered the following options, it didn’t ask until after the user had answered. A simple flush after the printf sorted this out.

Amanda6276
  • 33
  • 9