Choices on a menu

This is a discussion on Choices on a menu within the C++ Programming forums, part of the General Programming Boards category; Hi all When this is run, a menu of 2 choices is presented. The user has to select either 1 ...

  1. #1
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191

    Choices on a menu

    Hi all

    When this is run, a menu of 2 choices is presented. The user has to select either 1 or 2.

    Now if the user mistakenly select, for example 3 or 4, which is not on the menu, the program still runs.

    I'd like to make it that upon pressing other than 1 or 2, the program gives the menu again.

    If I need to use a do-while inside of a do-while, it seems awkward.

    How would I do it?

    Thanx

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
          char convert;
    
          do
          {
           cout.setf(ios::fixed);
           cout.setf(ios::showpoint);
           cout.precision(2);
    
           double usd, rm;
           int choice;
    
           cout << "What would you like to do?\n"
                << "1. Convert USD to RM.\n"
                << "2. Convert RM to USD." << endl << endl;
    
                //char choice;
    
                cin >> choice;
                cout << endl;
    
    
                if (choice == 1)
                {
                   cout << "Enter the value in USD you would like to convert: ";
                   cin >> usd;
    
                   rm = usd * 3.5;
    
                   cout << "USD " << usd << " is equal to RM " << rm << endl << endl;
                }
    
                if (choice == 2)
                {
                   cout << "Enter the value in RM you would like to convert: ";
                   cin >> rm;
    
                   usd = rm / 3.5;
    
                   cout << "RM " << rm << " is equal to USD " << usd << endl << endl;
                }
    
             //else
                 
    
             cout << "Would you like to convert currency again?\n";
             cin >> convert;
             }
    
    
             while (convert == 'y' || convert == 'Y');
                   cout << "Thank you\n";
    
    
             system("PAUSE");
             return 0;
    }
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Code:
    if(option == 1)
    
         //whatever
    if(option == 2)
    
        //deal with option 2
    
    else continue;
    This way, if the user doesn't press what s/he should, you'd skip the rest of the loop and go back to the start of the loop At least, in theory, in a program I made, it did the loop twice. But that's probably my fault.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Thanx but it doesn't seem to work...

    Upon entering 3 for example, the program goes straight to say "Press any key to continue" and then exits if a key is pressed.

    Code:
                if (choice == 1)
                {
                   cout << "Enter the value in USD you would like to convert: ";
                   cin >> usd;
    
                   rm = usd * 3.5;
    
                   cout << "USD " << usd << " is equal to RM " << rm << endl << endl;
                }
    
                if (choice == 2)
                {
                   cout << "Enter the value in RM you would like to convert: ";
                   cin >> rm;
    
                   usd = rm / 3.5;
    
                   cout << "RM " << rm << " is equal to USD " << usd << endl << endl;
                }
    
                 else continue;
                 
             }while (convert == 'y' || convert == 'Y');
                   cout << "Thank you\n";
    
    
             system("PAUSE");
             return 0;
    }
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try something like this:
    Code:
    // Menu example
    #include <iostream>
    #include <limits>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::flush;
    
    int main()
    {
        bool done = false;
    
        while (!done) {
            cout<<"1) Option 1\n2) Option 2\n";
            cout<<"Selection: "<<flush;
    
            int option;
            while (!(cin>> option)) {
                cin.clear();
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
                cout<<"Invalid input\nSelection: "<<flush;
            }
    
            if (option == 1)
                cout<<"Option 1"<<endl;
            else if (option == 2)
                cout<<"Option 2"<<endl;
            else {
                // Invalid
                cout<<"Invalid option, please try again"<<endl;
                continue;
            }
    
            done = true;
        }
    }
    It handles wildly invalid input as well as simply invalid options.
    My best code is written with the delete key.

  5. #5
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    I have this error file not found and it points to #include <limits>.
    I'm using Dev C++ 4.

    Thanx.
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Why not just put a do...while around the code you want to repeat if the user does not enter valid input like so?
    Code:
    do
    {
         //code you want to repeat
    }
    while (choice != 1 && choice != 2);
    Also, shouldn't you declare your variables outside the loop?
    Last edited by funkydude9; 08-13-2003 at 12:23 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I've never heard of <limits> before, but it seems to compile fine on my compiler (MSVC++...), try getting a new compiler. Or, just take the <limits> out and take out anything that needs <limits> to run... It looks like only std::numeric_limits<std::streamsize>::max(). Unless, of course, Prelude, that's an integral part of the program. I really can't say as I've never used it before

    >>Why not just put a do...while around the code you want to repeat if the user does not enter valid input like so?
    If I need to use a do-while inside of a do-while, it seems awkward.
    Apparently, he doesn't want to And besides, that wouldn't allow you to give the error message.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have this error file not found and it points to #include <limits>.
    You can remove limits if your compiler doesn't support it. (Odd, I seem to remember Dev-C++ 4 being relatively up to date). Anyway, make these changes:
    Code:
    while (!(cin>> option)) {
        cin.clear();
    
        int ch;
        while ((ch = cin.get()) != '\n' && ch != EOF)
            ;
    
        cout<<"Invalid input\nSelection: "<<flush;
    }
    My best code is written with the delete key.

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, 04:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM

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