Problem in Getting Menu Option

This is a discussion on Problem in Getting Menu Option within the C Programming forums, part of the General Programming Boards category; I'm currently doing an assignment but I've desperately to make this work: Code: int addString(void){ char addAnother, str[100]; do{ printf("Enter ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    6

    Question Problem in Getting Menu Option

    I'm currently doing an assignment but I've desperately to make this work:
    Code:
    int addString(void){
    
       char addAnother, str[100];
    
       do{
          printf("Enter string: ");
          gets(str);
    
          /* Some not important code here */
    
          printf("Add another? [Press Y for Yes]");
          addAnother = getchar();     /* Problem exist here */
       }while( (toupper(addAnother)) == Y );
    
       return 0;
    
    }
    Now, as I was asked to add another, I press 'y' then Enter and it will skip the gets() statement on top, like this:
    Output:
    ------------------------------------
    Add another? [Press Y for Yes]y
    Enter string: Add another? [Press Y for Yes]
    ------------------------------------

    I've tried to use getch() instead but end up the output like this:
    ------------------------------------
    Add another? [Press Y for Yes]
    Enter string: y
    ------------------------------------
    which the y always appear after my 'Enter string' prompt (which I didn't want).

    Is there any way that I can achieve this "menu selection" hassle?

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Fixed code:

    int addString(void)
    {
    char addAnother, str[100], character;

    gets(0);
    do
    {
    printf("Enter string: ");
    gets(str);

    /* Some not important code here */

    printf("Add another? [Press Y for Yes]");
    scanf("%c", &character);
    if (character != 'Y') then break;
    }while( (toupper(addAnother)) == Y );

    return 0;
    }

    I think this is it. That "gets(0);" is before do{}while loop beacuse it clears all the characters that "scanf" command left them, if you know what I mean.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Alright! Someone found a practical use for gets. Though getchar would still work in clearing the newline left by scanf.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    6

    Talking Thanks!

    Thanks for the help! The program works fine .

    (P/S: Now I know why scanf and gets can't live together in my assignments )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu Problem
    By vopo in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2007, 08:57 PM
  2. flat menu problem
    By valis in forum Windows Programming
    Replies: 0
    Last Post: 07-11-2005, 04:29 PM
  3. menu check problem
    By SuperNewbie in forum Windows Programming
    Replies: 5
    Last Post: 11-22-2003, 06:34 PM
  4. Context Menu cursor problem
    By dWorkVan in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2003, 12:42 PM

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