Going back to loop

Hello everyone, I've managed to tackle my assignment and complete it with the exception of one requirement, this program must loop back to the first menu if the users choses not to kick everyone out of the bus once its reached max capacity(50) this should take them back to the main menu and give them the option to terminate the program. .. I've set it to 5 so it does not have to loop so many times for testing purposes

I'm thinking my loops are sloppy and a nested loop is the solution but I'm a bit stumped, I appreciate your input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Counter
{
private:
    int count;
public:
    int getCount()const;
    void setCount(int count_);
    void output();

    void increaseCount()
    {
        count++;

    }

    Counter();
};

int main()
{

    Counter object1;
    int choice;
    char choice2;


    cout << "Welcome to the Bus! What would you like to do next?" << endl;
    cout << "1. Hop in"<< endl;
    cout << "2. Check the current number of people on board" << endl;
    cout << "3. Turn Around! Good bye!" << endl;

    cin >> choice;

    if (choice != 3 &&choice != 2 && object1.getCount() < 5)
    {

        do
        {
            object1.increaseCount();
            cout << "Make Another Selection" << endl;
            cout << "1. Hop in"<< endl;
            cout << "2. Check the current number of people on board" << endl;
            cout << "3. Turn Around! Good bye!" << endl;

            cin >> choice;

            cout << endl;
        }


        while(object1.getCount() < 5 && choice == 1 && choice != 3 );



        if (choice == 2)
        {

            cout << "Number of people on board: " << object1.getCount() << endl <<endl;

            exit(0);

        }
        else if (choice == 3)
        {
            exit(0);

        }
        else if (object1.getCount() >= 5)

            cout << "Crowded Bus! Kick Everyone out? Y or N ?" << endl;
        cin >>  choice2;

        if (choice2 == 'y' || choice2 == 'Y')
        {
            object1.setCount(0);

            cout <<"Everyone has been kicked out, there are now " << object1.getCount() << " people on the bus." << endl;
        }
        else
        {
            cout << "Sounds Good! No action taken" << endl;
        }
    }

    return 0;
}
Counter::Counter()
{
    count = 0;
}

void Counter::setCount(int count_)
{
    count = count_;
}

int Counter::getCount()const
{
    return count;
};

void Counter::output()
{
    cout << count;
}
Perhaps as a first revision, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

class Counter {
private:
	int count {};
public:
	int getCount() const { return count; }
	void setCount(int count_) { count = count_; }
	void increaseCount() { ++count; }

	Counter() {}
};

int main() {
	constexpr int maxbus {5};
	Counter object1;

	std::cout << "Welcome to the Bus!\n";

	for (int choice {}; choice != 3; ) {
		std::cout << "1. Hop in\n"
			<< "2. Check the current number of people on board\n"
			<< "3. Turn Around! Good bye!\n"
			<< "\nEnter option: ";

		std::cin >> choice;

		switch (choice) {
			case 1:
				if (object1.getCount() < maxbus)
					object1.increaseCount();
				else {
					char choice2 {};

					std::cout << "Crowded Bus! Kick everyone out? (Y or N): ";
					std::cin >> choice2;

					if (choice2 == 'y' || choice2 == 'Y') {
						object1.setCount(0);
						std::cout << "Everyone has been kicked out, there are now " << object1.getCount() << " people on the bus.\n";
					} else
						std::cout << "Sounds Good! No action taken\n";
				}
				break;

			case 2:
				std::cout << "Number of people on board: " << object1.getCount() << "\n\n";
				break;

			case 3:
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	}
}

You could put the menu into a value returning function - when the condition is met call the function and return 0 on the proper input
Topic archived. No new replies allowed.