-
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?
-
if you want to have it select more than once then you have to put it in a while loop
-
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;
}
-
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.