Put
cout<<endl<<"Wrong option number!! Try again\n";
cin.get(); //give the user a chance to read the output data
in a do-while -loop and change cin.get(); to cin>>option; like this:
do {
cout<<endl<<"Wrong option number!! Try again\n";
cin>>option;
} while(option<1 || option>2);
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
while ( cin>>option ) {
switch ( option )
{
}
}
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
while ( cin>>option ) {
switch ( option )
{
}
}
You have to take into account that the loop won't end automatically except if the user enters some text ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )
Why the h*ll would you use an integer flag? Isn't boolean good enough here ?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
flag is a variable name .. u can also write ur name instead of that.. its just that i am a bit convenient with that name that's its.....
I know flag is a variable name, but two things, if you would have to use a flag in this case you should have used a boolean instead of an integer and here it's just overkill, why would you use an integer flag here?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
i guess u didnt understand my code..... why dont you execute this code and see how exactly it works.......
and variable can also be put inside instead of boolean, any way 1= TRUE and and 0 or lesser implies false.......
I know, you don't have to teach me C++, but a boolean takes up less memory :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
What about the following:
do {
cin>>option;
switch ( option )
{
case 1:
displayCerteinDate();
break;
case 2:
displayAll();
break;
default:
cout<<endl<<"Wrong option number!! Try again\n";
}
} while (!(1<= option && option<=2));
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Did you even read my previous post ?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
yes i was trying your codes but ist giving error
looks fine to me but giging this error.
Then you must have done something wrong, I tested it and it was working perfectly :)
Could you please post the whole code which was causing this error?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Change
while (!(1<=option && option<=2));
<strong>}</strong>
to
<strong>}</strong>
while (!(1<=option && option<=2));
nt option; has to be int option; :)
the following:
....
cin>>option;
cin.get();
do {
....
should be changed to:
....
do {
....
and...
.....
default: cout<<endl<<"Wrong option number!! Try again\n";
cin.get(); //give the user a chance to read the output data
}
.....
has to be:
.....
default: cout<<endl<<"Wrong option number!! Try again\n";
}
.....
:P
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
It was actually good old Salem who brought me on the idea :P
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243