I am writing a program in C++, and I want the user to make a choice between a few options, and each option is different. At the end of the option, I want the user to be able to select a different option from a menu, but if the user originally chooses option 3, when the user gets back to the menu, if he/she chooses 1 or 2 it terminates the program. What can I do to make the code repeat itself?
#include <iostream>
using namespace std;
int main() {
int play;
cout << "What do you want to do now?" << endl;
cout << "Choose a number..." << endl;
cout << "1) Talk." << endl;
cout << "2) Vent." << endl;
cout << "3) Play a guessing game." << endl;
cout << "4) End." << endl;
cin >> play;
while (play == 1){
//code here
cout << "What do you want to do now?" << endl;
cout << "Choose a number..." << endl;
cout << "1) Talk." << endl;
cout << "2) Vent." << endl;
cout << "3) Play a guessing game." << endl;
cout << "4) End." << endl;
cin >> play;
}
while (play == 2){
//code goes here
cout << "What do you want to do now?" << endl;
cout << "Choose a number..." << endl;
cout << "1) Talk." << endl;
cout << "2) Vent." << endl;
cout << "3) Play a guessing game." << endl;
cout << "4) End." << endl;
cin >> play;
}
return 0;
}