-3

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;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
DZert
  • 3
  • 2

2 Answers2

1

The usual way to do something like this is to put your entire code in a while (true) loop, and break out of when the user chooses to exit, like so:

#include <iostream>
using namespace std;

int main() 
{
    int play;
    while (true)
    {
        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;

        if (play == 1)
        {
            // code for Talk...
        }
        else if (play == 2)
        {
            // code for Vent...
        }
        else if (play == 3)
        {
            // code for Play a guessing game....
        }
        else if (play == 4)
        {
             // End
            return 0;
        }
        else
        {
            std::cout << "Expected a number between 1 and 4" << std::endl;
        }
    }
}

EDIT I have also added a test for unrecognised input EDIT You can also use a switch statement, if you prefer the syntax (make sure you don't have fall through unless you really want it) (Note: both codes are equivalent and will probably produce identical assembly)

int main() 
{
    int play;
    while (true)
    {
        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;

    switch (play)
    {
    case 1:
        // code for Talk...
        break;
    case 2:
        // code for Vent...
        break;
    case 3:
        // code for Play a guessing game....
        break;
    case 4:
        // End
        return 0;
    default:
        std::cout << "Expected a number between 1 and 4" << std::endl;
    }
}
Isaac
  • 816
  • 5
  • 12
0

A fancy way to do this is using switch() and here is Why the switch statement and not if-else? A switch() can help your program get rid of having a very long nested if-else statements. Look how much beautiful its:

#include <iostream>

int main()
{
    int play = 0;

    while (play != 4)
    {
        std::cout << "What do you want to do now?" << std::endl;
        std::cout << "Choose a number..." << std::endl;
        std::cout << "1) Talk." << std::endl;
        std::cout << "2) Vent." << std::endl;
        std::cout << "3) Play a guessing game." << std::endl;
        std::cout << "4) End." << std::endl;
        std::cin >> play;

        switch (play)
        {
            case 1: // code for talk here
                break;
            case 2: // code for Vent
                break;
            case 3: // code for Play
                break;
            case 4: std::cout << "program will exits!";
        }
    }
    return 0;
}
Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104