C++ Programming Looping

do
{
cout<<"When did the call start?\n";
cout<<"Please enter the time in 24-hour notation.\n";
cin>>start_time;
cout<<"How long did the call last?\n";
cin>>minutes;
cout<<"Please enter the first two letters of the day of the call.\n";
cin>>first>>second;

if ((((first=='M')||(first=='m'))&&((second=='O')||(second=='o'))) || (((first=='T')||(first=='t'))&&((second=='U')||(second=='u'))) || (((first=='W')||(first=='w'))&&((second=='E')||(second=='e')))
|| (((first=='T')||(first=='t'))&&((second=='H')||(second=='h'))) || (((first=='F')||(first=='f'))&&((second=='R')||(second=='r'))))
{
if ((start_time>=800) &&(start_time<=1800))

{
cost=minutes*.40;
}
else
{
cost=minutes*.25;
}
}

else if ((((first=='S')||(first=='s'))&&((second=='A')||(second=='a'))) || (((first=='S')|| (first=='s'))&& ((second=='U')||(second=='u'))))
{
cost=minutes*.15;
}

else
{


cout<<"\nYour response is invalid. Please try again."<<endl;
cout << "Please enter the day of the call (Mo/Tu/We/Th/Fr/Sa/Su):";
cin >> first>>second;

}




How do I make the BOLD part to continuously prompt user until a valid response is given?

I tried using boolean, do-while, but I can't seems to make it work.

Please help.
Last edited on
It's good to write it down. What you really want is this:
1
2
3
4
input:
cin >> something;
if(isGood(something)) doSomething();
else goto input;


But then we know that goto is evil so we now replace this by a loop. There must be a million ways to write it...

1
2
3
4
5
while(true) {
   cin ...
   if (good) {doStuff; break;}
   else ;
}


1
2
3
4
5
6
while(true) {
   cin ...
   if (good) doStuff;
   else continue;
   break;
}


1
2
3
4
5
6
bool valid = true;
do{
   cin ...;
   if(good) doStuff;
   else valid = false;
} while(!valid);


And also a bit different and maybe less flexible one:
1
2
3
4
5
while(true) {
   cin ...
   if(good) break;
}
doStuff;


1
2
3
do cin ...;
while (!good);
doStuff;
Last edited on
Topic archived. No new replies allowed.