Thread: show the menu only once each time..

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Laserlight is quite right, I misunderstood your problem, Transgalactic2.

    In an earlier problem, I mentioned (and perhaps others did as well), that adding a getchar() after a scanf(), pulled the troublesome newline off the keyboard buffer.

    In this case, the newline isn't the problem, Transgalactic2.

    Here's a menu version that may suit you better. On bad input, it just prints the last line, one additional time, no matter how many letters you have of bad input there may be.

    Also, it doesn't use conio.h, or any functions from conio.h.

    Code:
    #include <stdio.h>
    
    int main(void)  {
       int gar; 
       int choice = 0;
       int menu(void);
    
       choice = menu();
       printf("\n  Your Choice Was %d ", choice);
       printf("\n\n\t     Program Complete - Press Enter When Ready \n");
       
       gar = getchar(); gar++;
       return;
    }
    int menu(void)  {
       int i, gar; 
       int number = 0;   
       char a1 = 178;
       char a2 = 177;
       char a3 = 176;
       
       printf("\n\t\t %c%c%c Welcome to the Main Menu %c%c%c\n\n\n", a1,a2,a3, a3,a2,a1);
       printf("\t Please Choose One of the Following Choices: \n\n");
       printf("\t 1) Play a Game \n");
       printf("\t 2) View The Highest Scores \n");
       printf("\t 3) View All Scores \n");
       printf("\t 4) Explore Game Options and Characters \n");
    
       do  {
          printf("\t    Your Choice [1-4]: ");
          scanf("%d", &number);
          while((gar = getchar()) != '\n');  //this line pulls off all the bad input
       }while(number > 4 || number < 1);
    
       return number;
    }

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM