-1

I have the below code which compiles however I would like it to loop back to the original menu after the user has selected a choice so another can be picked.

Any help would be greatly appreciated.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int choice;
    cout<<"Select your favourite soft drink:\n";
    cout<<"Pepsi - 1\n";
    cout<<"sprite - 2\n";
    cout<<"fanta - 3\n";
    cin>>choice;

    if(choice==1)
    {
        cout<<"Good Choice"<<endl;
    }
    else if(choice==2)
    {
        cout<<"Not bad"<<endl;
    }
    else if(choice==3)
    {
        cout<<"ew!"<<endl;
    }

    getch();
    return 0;
}
David
  • 27,652
  • 18
  • 89
  • 138
user3536870
  • 249
  • 1
  • 2
  • 13

2 Answers2

0
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
  for(;;){ // <-- loop started here
    int choice;
    cout<<"Select your favourite soft drink:\n";
    cout<<"Pepsi - 1\n";
    cout<<"sprite - 2\n";
    cout<<"fanta - 3\n";
      cout<<"exit - 4\n"; // <-- break the loop with a 4
    cin>>choice;
    if(choice==1)
    {
    cout<<"Good Choice"<<endl;
    }
      else if(choice==2)
      {
        cout<<"Not bad"<<endl;
      }
      else if(choice==3)
      {
         cout<<"ew!"<<endl;
      }
      else if(choice==4)
      {
         break;
      }    
  } // <-- loop end here
  getch();
  return 0;
}
Martin G
  • 17,357
  • 9
  • 82
  • 98
  • Why not use a `while` loop? It is designed to be used in loops where the number of repetitions is unknown. Or better a `do while` loop because the choice has to be presented at least once. A `for` loop isn't a good choice for this situation imho. – Skalli Apr 28 '14 at 13:57
  • Yes, if the example looked nicer to begin with i would agree. I usually try to keep the input decoding in ONE place and here it is in the if statement already implemented in the example. Adding flags or breaking a while loop instead of a foor loop is not better imo. But then i'm only talking about this particular case. – Martin G Apr 28 '14 at 14:04
  • Especially since the example didn't look that nice. We obviously deal with a beginner and in that case an answer with nicer code and an explanation of the choices and why we choose one would be nicer. Or a link to a duplicate question. I doubt the questioner has learned much from the answers. (Which is no accusation, just a hint.) – Skalli Apr 28 '14 at 18:14
0
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int choice;
    do{ //Start Loop

        cout << "Select your favourite soft drink:\n";
        cout << "Pepsi - 1\n";
        cout << "sprite - 2\n";
        cout << "fanta - 3\n";
        cin >> choice;

        if (choice == 1)
        {
            cout << "Good Choice" << endl;
        }
        else if (choice == 2)
        {
            cout << "Not bad" << endl;
        }
        else if (choice == 3)
        {
            cout << "ew!" << endl;
        }
    } while (choice != 4); // Keep looping until the input is 4.
    return 0;
}
JustinCase
  • 43
  • 6