Disallowing an option from repeating itself?

Hello again, im writing a program where there are a number of cases which are constantly repeated in a do-while loop until the user decides to end the program, but I cant find a way to disallow the repeating of a particular choice, say, choice a' being chosen again instead of another choice, b,c,d, etc. there is a switch statement after the 'do' portion with the list of cases and their respective outputs, but im not sure how to disallow a case from being chosen more than once. Thanks for reading.

If you mean again (as in preventing twice in a row) then just store the last option used in a variable and compare with the next option.

If you mean after say option 1 is pressed then you cant do it again then you can just do something simple like have a boolean variable set to true once the option has been run and checking that bool would prevent it from running again.
I believe I mean the latter
this is an example of choice a's code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch (choice)
			{
					case 'a':
							cout << "Enter an even bet: ";
							double evenBet;
							cin >> evenBet;
							if (evenBet >= 0.00 && evenBet < startingCash)

							{
								newTotal = startingCash - evenBet;
								cout << name << ", you have $" << newTotal << " left" << endl;
							}

							if (evenBet > startingCash)
							{
								cout << endl; cout << "Cannot bet that much! Try again." << endl;
							}
								else if (evenBet < 0)
								{	
									cout << endl; cout << "Invalid bet! Try again." << endl;
								}
							
							break;


I cant get around how to set up a bool to allow it once and not allow it a second time. would I t be something like initializing constant bools for each choice, a-d, and making a long "if" statement that diallows any repetition, with something like an initialized constant bool named choice and saying
1
2
if (choiceA > 1)
cout<< "Try another choice you haven't used"<<endl;
?
thanks for such a quick reply too!
Last edited on


Try 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

#include <iostream>
#include <ctype.h>		// toupper
using namespace std;

int main()
{
	
	bool opt1 = false, opt2 = false;
	char choice, running = true;

	while (running)
	{

		do {
			cout << "Enter Choice (a, b or c): ";
			cin >> choice;
			choice = toupper(choice);
		} while (choice < 'A' || choice > 'C');


		switch (choice)
		{
		case 'A':
			if (!opt1) // if not true
			{
				opt1 = true;	// stop it happening again.
				cout << "Option A" << endl;
				// rest of code.
			}
			else
				cout << "Already used!!" << endl;
			break;
		case 'B':
			if (!opt2)
			{
				opt2 = true;
				cout << "Option B" << endl;
				// rest of code
			}
			else
				cout << "Already used!!" << endl;
		break;
		case 'C':
			cout << "Option C" << endl;
			running = false;	// stop program
		break;
		
		}

	} 
	return 0;
}
I tried rewriting it that way, but I don't think it applies directly to how the program should flow in my case. Here is the total code I have so far. It is a homework assignment, so some general guidelines and hints are appreciated rather than me simply plagiarizing any code you give out to me. So far this runs well, but doesn't disallow for the input of a variable once it has already been entered. thanks
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
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	using std::cout;
	using std::cin;
	const char QUIT = 'q';
	char choice;
	string name;
	const int MAX_NUMER = 37;
	const int DOUBLE_ZERO = 00;
	const int DEFAULT_PLAYER_NUMBER = DOUBLE_ZERO + 1;
	const int EVEN_ODD_PAYOUT = 1;
	const int STRAIGHT_UP_PAYOUT = 35;
	double startingCash = 1000.00;
	
	bool choiceA = false;
	bool choiceB = false;
	bool choiceC = false;
	bool choiceD = false;
	cout << "What is your name? ";
	getline(cin, name);
	
	do {
		cout << endl;
		cout << "Roulette 1.0" << endl;
		cout << endl;
		cout << fixed<< setprecision(2) << showpoint;
		cout << endl;
		cout << name << ", you have $" << startingCash << " left.";
		cout << endl;
		cout << "Select from the following options: ";
		cout << endl;
		cout << "(a) Even Bet " << endl;
		cout << "(b) Odd Bet " << endl;
		cout << "(c) Straight Up Bet " << endl;
		cout << "(d) Spin the Wheel " << endl;
		cout << "(q) Quit " << endl;
		cout << endl;
		cout << "Enter the letter of your choice: ";
		cin >> choice;
		
		switch (choice)
		{
		case 'a':
			cout << "Enter an even bet: ";
			double evenBet;
			cin >> evenBet;
			
			if (!choiceA)
				choiceA = true;
			if (evenBet >= 0.00 && evenBet <= startingCash)
			{

				{
					startingCash = startingCash - evenBet;
					
				}

				if (evenBet > startingCash)
				{
					cout << endl; cout << "Cannot bet that much! Try again." << endl;
				}
				else if (evenBet < 0)
				{
					cout << endl; cout << "Invalid bet! Try again." << endl;
				}
			}
		else
								cout << "ENter something you haven't used, try again.";
							break;


			case 'b':
						cout << "Enter an odd bet: ";
						double oddBet;
						cin >> oddBet;
						
						if (!choiceB)
							choiceB = true;
						if (oddBet >= 0.00 && oddBet < startingCash)
						{
							startingCash = startingCash - oddBet;
						
							}

							if (oddBet > startingCash)
							{
								cout << endl; cout << "Cannot bet that much! Try again." << endl;
							}
							else if (oddBet < 0)
							{
								cout << endl; cout << "Invalid bet! Try again.";
							}
						
						break;

			case 'c':
							cout << "Enter a straight up bet" << endl;

							break;


			case 'd':
							cout << "Blah blah blah " << endl;
							break;
									}
							}

	while (choice != QUIT);
		cout << "Thanks for playing, " << name << "!"<<endl;
		cout << endl;

		

		
}

Look at the following modified code and compare. The code I suggested will work fine if its put in the correct area.

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
113
114
115
116
117
118
119
120

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	using std::cout;
	using std::cin;
	const char QUIT = 'q';
	char choice;
	string name;
	const int MAX_NUMER = 37;
	const int DOUBLE_ZERO = 00;
	const int DEFAULT_PLAYER_NUMBER = DOUBLE_ZERO + 1;
	const int EVEN_ODD_PAYOUT = 1;
	const int STRAIGHT_UP_PAYOUT = 35;
	double startingCash = 1000.00;

	bool choiceA = false;
	bool choiceB = false;
	bool choiceC = false;
	bool choiceD = false;
	cout << "What is your name? ";
	getline(cin, name);

	do {

		cout << endl;
		cout << "Roulette 1.0" << endl;
		cout << endl;
		cout << fixed << setprecision(2) << showpoint;
		cout << endl;
		cout << name << ", you have $" << startingCash << " left.";
		cout << endl;
		cout << "Select from the following options: ";
		cout << endl;
		cout << "(a) Even Bet " << endl;
		cout << "(b) Odd Bet " << endl;
		cout << "(c) Straight Up Bet " << endl;
		cout << "(d) Spin the Wheel " << endl;
		cout << "(q) Quit " << endl;
		cout << endl;
		cout << "Enter the letter of your choice: ";
		cin >> choice;

		switch (choice)
		{
			case 'a':
				// is choiceA false?
				if (!choiceA)
				{
					// yes, we havent run before, set to true
					// to prevent this option running again.
					choiceA = true;
					cout << "Enter an even bet: ";
					double evenBet;
					cin >> evenBet;
					if (evenBet >= 0.00 && evenBet <= startingCash)
					{
						startingCash = startingCash - evenBet;

						if (evenBet > startingCash) {
							cout << endl; cout << "Cannot bet that much! Try again." << endl;
						}
						else 
							if (evenBet < 0) {
								cout << endl; cout << "Invalid bet! Try again." << endl;
						}
					}
					else
						cout << "ENter something you haven't used, try again.";
				}
			break;


			case 'b':
				if (!choiceB)
				{
					choiceB = true;
					cout << "Enter an odd bet: ";
					double oddBet;
					cin >> oddBet;

					if (oddBet >= 0.00 && oddBet < startingCash)
						startingCash = startingCash - oddBet;

					if (oddBet > startingCash) {
						cout << endl; cout << "Cannot bet that much! Try again." << endl;
					}
					else if (oddBet < 0) {
						cout << endl; cout << "Invalid bet! Try again.";
					}
				}
			break;

			case 'c':
				if (!choiceC) {
					choiceC = true;
					cout << "Enter a straight up bet" << endl;
				}
			break;

			case 'd':
				if (!choiceD)
				{
					choiceD = true;
					cout << "Blah blah blah " << endl;
				}
			break;
		}
	
	}while (choice != 'q');
	
	cout << "Thanks for playing, " << name << "!" << endl;
	cout << endl;
}
Topic archived. No new replies allowed.