show the menu only once each time..

This is a discussion on show the menu only once each time.. within the C Programming forums, part of the General Programming Boards category; when i input a number which is out of range it prints the menu again so in a single letter ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    show the menu only once each time..

    when i input a number which is out of range it prints the menu again so in a single letter case.
    but when i input "abc" it prints the menu 3 times in fact the number of menu print
    depends on the number of chars in the strings.
    how to do tat no matter what wrong input i will give it,it will print the menu again only once??
    Code:
    #include <stdio.h>
    
    #define N 9
    
    void mainMenu();
    
    
    int main()
    {
        char input[40];
        char input2[40];
        char board[N][N];
        mainPlayGame(board);
        printf("bye, please press enter to exit!\n");
       getchar();
       return 0;
    }
    
    void mainPlayGame(board){
        int opt,leng,size=-1;
      int index,kndex;
        char input[40];
        char ch_cords[40];
        char input2[40];
        int tr;
        int cords[8][2];
        int tndex;
    
        int i,k,j,ch,l;
    
        int  leng2;
        do
        {
          mainMenu();
    
        scanf("%d",&opt);
        l=getchar();
         if (opt==1)
         {
             printf("1");
         }
         if (opt==2)
        {
            printf("2");
         }
         if (opt==3)
         {
              printf("3");
         }
         if (opt==4)
         {
            printf("4");
         }
         if (opt==5)
        {
           printf("5");
         }
         if (opt==0){
         }
        }while(opt!=0);
    }
    
    int playGame(char board[N][N],int size){
    
    }
    
    void mainMenu() {
    
    
     printf("--------------------------\n");
    printf("welcome to the game\n");
    printf("1. choose board size\n");
    printf("2. place mines\n");
    printf("3. remove mines\n");
    printf("4. show mines\n");
    printf("5. start the game\n");
    printf("0. exit\n");
    printf("please enter your choice (input control is needed): \n");
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    how to do tat no matter what wrong input i will give it,it will print the menu again only once??
    "Flush" the input buffer after you read the option. This is covered by the FAQ, and in fact it has been shown to you in at least one previous thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    cant use flush

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need it to print it again only once no matter what illegal input

    here is what i get
    http://img49.imageshack.us/my.php?image=27465407kf1.gif
    maybe is because of the scanf
    ??

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is there a way ??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    cant use flush
    What do you mean? If you are not allowed to "flush" the input buffer as described in the FAQ, then your problem cannot be solved, because to solve it is against the rules imposed on you.

    If you think that I am recommending the use of fflush(), then you have not read the FAQ.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is one way to do what you want, but note that it uses the non-standard header file, conio.h, to provide unbuffered input.

    Other options include printing up the whole menu except the one line that shows them the choice. In case of error, a while loop can just reprint the last line and scanf(), again. So your bad entry would look like this:

    Your Choice [1-4]: 5 <enter>
    Your Choice [1-4]: 2 <enter>

    etc.

    So only that one line would be reprinted, for each bad entry, not the whole menu.

    You could also just print out 40 newlines, which would blank the screen out, and just reprint the whole menu (seems odd, but it works).

    Anyway, here's how you make people who strictly love the C standard, faint in their tracks:

    Code:
    #include <stdio.h>
    #include <conio.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");
       printf("\n         Your Choice [1-4]: ");
       do  {
          number = getche() - 48;
          //scanf("%d", &number);
          getch();
          if(number < 0) number = 0;  //an error occurred
          if(number < 1 || number > 4) 
                printf("%c%c%c", '\b', ' ', '\b');
       }while(number > 4 || number < 1);
    
       return number;
    }
    There are other low-level options including using conio.h to locate your current screen position, and just back it up one row, then print a line of 79 char's, and then return to the start of that line again, and print your "Your Choice [1-4]: line, again.

    Another option is working directly with your video memory to poke in the right values - probably a pain in Windows, though.

    Another option is to use the Windows API which provides a function for moving the cursor on the monitor.

    Using getchar() won't help here. You're dealing with buffered input, and as soon as the <enter> key is hit, the monitor will move to the next line.
    Last edited by Adak; 01-04-2009 at 10:54 AM.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i use gets??

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    can i use gets??
    Yes you can, no you may not.

    I think Adak misinterpreted your requirements since you seem perfectly fine with printing the menu whenever there is invalid input, but the point is that you only want to print it once.

    As you are apparently unable to find the correct FAQ item, here it is: How do I flush the input buffer?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i added getchar bellow the scanf

    for "abc" its still printing the menu several times
    instead of once

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    i added getchar bellow the scanf

    for "abc" its still printing the menu several times
    instead of once
    Why did you add the getchar below the scanf? What do you think the getchar does?

    The FAQ suggests more than that, and even what the FAQ suggests cannot be taken blindly since you must adapt it for your situation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    getschar clears the buffer

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by transgalactic2
    getschar clears the buffer
    No, getchar() gets the next character from the buffer, or returns EOF if there is none. How many characters are on the buffer after the user enters "abc"? How many are left after the scanf() fails to scan an integer?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    after scanf() fails to scan an integer there are 2 characters in the buffer

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so
    can i build a loop
    of
    Code:
    while there are characters in the buffer{
     
    num=getchar();
    }
    get char will take one character out of the buffer each time

    is that correct?

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21