How can I stop my code from breaking

#include

using namespace std;

int invitedGuests;

int op1() {
cout << "How many guests would you like to invite?: ";
cin >> invitedGuests;

return invitedGuests;

}

void op2(int guests) {
int cards = guests / 2;
double sweets = guests * 1.2;
cout << cards << “invitation cards” << endl
<< sweets << “sweets”;

}

void menu() {
cout << “1. Enter number of invite guests.” << endl
<< “2. Determine the number of invitation cards and sweets” << endl
<< “3. Determine the number of tables needed” << endl
<< “4. Determine drinks order” << endl
<< “5. Cost of invitation cards.” << endl
<< “6. Cost of drinks” << endl
<< “7. Display all information” << endl
<< “8. Quit” << endl;
}

int userChoice() {
menu();
int choice;
cout << "Choose an option: ";
cin >> choice;
do {
if (choice == 1) {
cout << “You selected 1 (invite guests).” << endl;
op1();
break; }
else if (choice == 2){
cout << “You selected 2 (invitation cards and sweets).” << endl;
op2(invitedGuests);
break; }
else if (choice == 3) {
cout << “You selected 3 (number of tables needed)” << endl;
break; }
else if (choice == 4) {
cout << “You selected 4 (drinks order).” << endl;
break; }
else if (choice == 5) {
cout << “You selected 5 (cost of invitational cards).” << endl;
break; }
else if (choice == 6) {
cout << "You selected 6 (cost of drinks). " << endl;
break; }
else if (choice == 7) {
cout << “You selected 7 (Display all information)” << endl;
break; }
else if (choice == 8) {
cout << “You selected 8 (Quit).” << endl;
break; }
} while (choice != 8);

return 0;

}

int main() {
cout << “Hi, the Wedding Planning Assistant is pleased to be at your service!” << endl;

int runProgram = userChoice();
cout << runProgram;

return 0;

}

I want to be able to choose a new option after I’ve entered number of invited guests, but now it just breaks. How can I do this?

Could you put that code into a code fence so it is easier to read? Like this:

```
Your code here
```

Also, when you say “it just breaks” can you copy the error you are getting? Ideally show the sequence of steps you are doing and what result you expected vs what you actually got.

1 Like

You need a ‘game loop’

So it will loop back up

it can be any loop, I think do-while loop would suit best. Just put everything in your main inside it.

You should also make a question at the end if the user wants to try again or leave.

so like this:

bool wannaQuit = false;

int main()
{

do{
everything in main currently except the main() and {} which belong to main

string again;
cout << “\n do you want to quit or go again? Q/G”;
cin >> again;
if(again == “Q” || again ==“q”) { wannaQuit = true; }
if(again == “G” || again ==“g”) { wannaQuit = false; }

}while(wannaQuit == false);
}//main end