Ok so I have a menu program here which works fine. I'm trying to account for error input by the user. I'm unsure how I let the user enter an input again if they accidently made a mistake. I know I have to use loops.
// Example program
#include <iostream>
#include <string>
usingnamespace std;
void play_movie()
{
cout << "you are now playing!";
}
void pause_movie()
{
cout << "you have now paused the movie!";
}
void rewind_movie()
{
cout << "you have now rewinded the movie!";
}
void forward_movie()
{
cout << "You have now forwarded the movie!";
}
int main()
{
int input;
cout << "1) Play movie \n";
cout << "2) Pause movie \n";
cout << "3) Rewind movie \n";
cout << "4) Forward movie \n";
cout << "5) What do you want to do: ";
cin >> input;
switch (input)
{
case 1:
play_movie();
break;
case 2:
pause_movie();
break;
case 3:
rewind_movie();
break;
case 4:
forward_movie();
break;
default:
cout << "Invalid Input";
}
}
int input;
do
{
cout << "1) Play movie \n";
cout << "2) Pause movie \n";
cout << "3) Rewind movie \n";
cout << "4) Forward movie \n";
cout << "5) Quit the program: \n\n";
cout << "What do you want to do: \n";
cin >> input;
switch (input)
{
case 1:
play_movie();
break;
case 2:
pause_movie();
break;
case 3:
rewind_movie();
break;
case 4:
forward_movie();
break;
case 5:
cout << "The program has now been terminated. Bye.. \n";
break;
default:
cout << "Invalid Input\n\n";
}
}while(input !=5);
// Example program
#include <iostream>
#include <string>
usingnamespace std;
void play_movie()
{
cout << "you are now playing!";
}
void pause_movie()
{
cout << "you have now paused the movie!";
}
void rewind_movie()
{
cout << "you have now rewinded the movie!";
}
void forward_movie()
{
cout << "You have now forwarded the movie!";
}
int main()
{
int input;
do
{
cout << "1) Play movie \n";
cout << "2) Pause movie \n";
cout << "3) Rewind movie \n";
cout << "4) Forward movie \n";
cout << "5) What do you want to do: ";
cin >> input;
switch (input)
{
case 1:
play_movie();
break;
case 2:
pause_movie();
break;
case 3:
rewind_movie();
break;
case 4:
forward_movie();
break;
default:
cout << "Invalid Input" << endl;
}
}
while((input > 4) || (input < 1));
cout << endl << endl;
system ("PAUSE");
return 0;
}