1

Header file:

    #include <stdio.h>
    #include <stdlib.h>

    int print_menu(){
      printf ("MENU\n");
      printf ("1. Total of All Top Scores for the Week\n");
      printf ("2. Total of All High Scores for the Week\n");
      printf ("3. Total Machine High Scores for the Week\n");
      printf ("4. Machine High Score for the Week\n");
      printf ("5. EXIT\n");
      printf ("Enter Selection:");

      int selection = getchar();
      return selection;
    }

Main C file:

    #include <stdio.h>
    #include <stdlib.h>
    #include "lab1.h"

    int main(int argc, char *argv[]){
      int selection = print_menu();
      while(1)
      {
        switch (selection)
        {
          case '1':
            printf ("\nselected 1\n");
            break;
          case '2':
            printf ("\nselected 2\n");
            break;
          case '3':
            printf ("\nselected 3\n");
            break;
          case '4':
            printf ("\nselected 4\n");
            break;
          case '5':
            printf ("\nExit\n");
            exit(0);
            break;
          default:
            printf ("Invalid Selection");
            print_menu();
            break;
        };
      };
    }

My issue is that when I run the program and enter in a wrong character the program is suppose to re-print the menu and ask for the selection again. Except it prints out the menu twice. Example:

    maiah@maiah-vb:~/shared$ ./a.out
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:d
    Invalid Selection
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:
    Invalid Selection
    MENU
    1. Total of All Top Scores for the Week
    2. Total of All High Scores for the Week
    3. Total Machine High Scores for the Week
    4. Machine High Score for the Week
    5. EXIT
    Enter Selection:

And then you are given the option to enter in another selection. When I went through I noticed that it seems to be taking the selection of 'd' and outputting correctly but then acting like a blank space or new line has been automatically entered and proceeds to check the selection (I'm not sure if this is the actual issue though - this is just how it seems to be acting). If anyone has any ideas on how to fix this and explain why this is happening. Any help would be great!

1
  • 2
    Don't put code in headers. Commented Sep 18, 2019 at 5:18

3 Answers 3

2

You need to keep what your printing function returns.

      default:
        printf ("Invalid Selection");
        selection = print_menu();
        break;

The second problem is that your getchar() call will also take the return after your selection. Add another getchar() to consume it.

int selection = getchar();
(void) getchar(); /* ignore enter key */
return selection;

On a side note, do not put code into headers, only declarations.
Otherwise the code will be compiled in each code file (of several) which includes the header and get you multiple definition errors. This is not apparant if you have only one code file including the header, but you should get into the right habit early.

Finally you need to always read in again, not only in case of 5.

      default:
        printf ("Invalid Selection");
        break;
    };
    selection = print_menu();
}; /* end of while */

I.e. do it within the loop but outside the case statement(s), because the default branch is only taken if none of the other was executed.

Sign up to request clarification or add additional context in comments.

3 Comments

I would suggest using scanf("%c", &selection); instead of getchar
@vx3r OK, make an answer to elaborate the advantages.
@Yunnosch thank you so much for your explanations! I went through and made the changes. It is working now the way I need it too and I moved my print function into the c file!
0

try

  while(1)
  {

      int selection = print_menu();
      switch (selection)
      ....
  }

1 Comment

That helps with most of the problems. But please write an explanation of your code to help fighting the misconception that StackOverflow is a free code writing service, which is otherwise spread by code only answer like this. Also "Try..." is giving the impression of "I am not sure whether this helps.". I recommend trying for more assertive answers.
0

getchar() has the side effect of removing the next character from the input buffer.

Since the scanf() is told to read one and only one character (%c) this has the effect of ignoring everything else on that input line.

Also clear the console before reprinting the menu.

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

   int print_menu()
   {
    char selection;

    printf("MENU\n");
    printf("1. Total of All Top Scores for the Week\n");
    printf("2. Total of All High Scores for the Week\n");
    printf("3. Total Machine High Scores for the Week\n");
    printf("4. Machine High Score for the Week\n");
    printf("5. EXIT\n");
    printf("Enter Selection:");

    scanf("%c", &selection);

    return selection;
   }

   int main(int argc, char *argv[])
   {
    int selection;

    while(1) {
      selection = print_menu();

      switch (selection)
      {
        case '1':
          printf("\nselected 1\n");
          exit(selection);
          break;
        case '2':
          printf("\nselected 2\n");
          exit(selection);
          break;
        case '3':
          printf("\nselected 3\n");
          exit(selection);
          break;
        case '4':
          printf("\nselected 4\n");
          exit(selection);
          break;
        case '5':
          printf ("\nExit\n");
          exit(0);
          break;
        default:
          system("@cls||clear");
          printf("\nInvalid Selection\n");
          break;
      };
    };
   }

3 Comments

"getchar() has the side effect of removing the next character from the input buffer" Yes, same for scanf. "scanf() is told to read one and only one character (%c) this has the effect of ignoring everything else on that input line" yes, getchar also ignores everything beyond the single letter.
Did you test this program? I did. It never loops, whatever you enter. If you remove the several exit(selection) it does loop but always (except 5) output "Invalid Selection." (I guess with a short flash of the desired output for the very first selection<5 entered. ) The root cause might be found in sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
If you additionally remove the system() call you can see the desired output for e.g. 1, followed immediatly by the menu and "Invalid Selection". See the article I linked for the explanation, in short, scanf() does ignore the rest of the line, as you correctly stated, it does however also not consume it - exactly like getch().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.