Thread: Menu Help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Menu Help

    Im trying to make a menu but i keep getting one problem that im not sure how to fix.

    My code is something like this.

    Code:
                 cout<<"1=blabla"<<endl;
                 cout<<"2=blabla"<<endl;
                 cout<<"Choose a number"<<endl;
                 cin<<select; 
                 if(select==1){
                     blablabla
                      }
                 if(select==2){
                  bla blabla
                    }
    Now the problem is that if i pick 2 then if i let it reset select to 0 and pick another number like 1 it thenjust shuts off because i think its reading it form top down so once it goes to 2 it cant move back up to one. How would i go around this?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    if you want to have it select more than once then you have to put it in a while loop

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    ok, you're trying to get the same menu to come up correct?

    put in something like

    Code:
    while( select > 0 && select < 3 )
    {
        cin<<select;
        all the other stuff;
    }

  4. #4
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int select; //the actual selection you'll be inputing
        int selectCount = 0; //you'll see why i made this later. make sure it's initialized
        
        while (selectCount < 1) //while the variable selectCount is zero
        {
              cout<<"Choice: "; //displays choice
              cin>>select; //input number for the variable select
              cin.ignore() //doesnt close the program when enter is pushed
              
              if (select == 1) //if you type in 1 for the select variable
              {
                         cout<<"\nBla, bla, I pick one!\n"; //what is displayed when 1 is assigned to select
                         selectCount++; //adds 1 to selectCount, making it 1 and ending the while loop
              }
              else if (select == 2) //if you type in 2 for the select variable
              {
                     cout<<"\nBla, bla, I pick two!\n"; //what is displayed when 2 is assigned to select
                     selectCount++; //adds 1 to selectCount, making it 1 and ending the while loop
              }
              else //if anything else is input
              {
                  cout<<"\nInvalid Choice!\n\n"; //what is displayed when anything besides 1 or 2 is input
              } //notice there is no selectCount++; , because we dont want the loop to end until 1 or 2 is input
        }
        
        cin.get(); //push enter before program closes
        return 0; //and voila!
    }
    I hope that helps.
    ------------------------------------------------
    cout<<"Hello World!\n";
    ------------------------------------------------

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. 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, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM