Issues with switch statements

Ok here is what needs to be done.

• Create a do loop that asks the user if they want to roll the dice (yes or no) until the user says no.
• At the beginning of the loop ask the user how many sides the dice has (either 4, 6, 8, 10, 12, or 20).
• Use the rand() function and output the statement "You rolled a " and then a random number that can be found on the type of dice they chose. So a 6 sided dice can give the number 1 through 6.
• Use a switch statement to check the user input for the sides of dice. There should be 6 case statements (1 for each type of dice).
• Just before the program exits print out an exit statement.


I dont know if Im doing it right or if I understand what he is looking for in the switch statements. I am so lost didnt help I dont have the class book yet. Dam book store. any help is greatly appreciated!

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
  
#include <iostream>		
#include <ctime>		
using namespace std;
	

int throwDie(int Sides, int &throwResult)
{
	throwResult = 1 + rand() % (Sides - 1 + 1); 
	return throwResult;						 
}
int main()
{			
	int dieTot = 0,
		throwNumber = 0,
		numberSides = 0,
		throwResult = 0;
	char rollAgain;
	srand(unsigned(time(0) ));					   
	cout << "Do you want to throw a die?" << endl;
	cin >> rollAgain;
	do{
		cout << "How many sides? 4, 6, 8, 10, 12, 20" << endl;
		cin >> numberSides;
        cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cout << "Do you want to throw a die?" << endl;
		cin >> rollAgain;
	}while(toupper(rollAgain) == 'Y');


	switch( numberSides )
	{
		case 1:
			cout << "You have choosen a 4 sided die? ";
			cin >> numberSides;
			break;
		case 2:
			cout << "You Choosen a 6 sided die? ";
			cin >> numberSides;
			break;
		case 3:
			cout << "You Choosen a 8 sided die? ";
			cin >> numberSides;
			break;
		case 4:
			cout << "You Choosen a 10 sided die? ";
			cin >> numberSides;
			break;
		case 5:
			cout << "You Choosen a 12 sided die? ";
			cin >> numberSides;
			break;
		case 6:
			cout << "You Choosen a 20 sided die? ";
			cin >> numberSides;
			break;
		default:
			cout << "Not a proper entry.\n";
			break;
	cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
	return 0;
}

}
Last edited on
here is where I am at now the only error I recieve is while expected line 125

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
122
123
124
125

#include <iostream>		
#include <ctime>		
using namespace std;
	

int throwDie(int Sides, int &throwResult)
{
	throwResult = 1 + rand() % (Sides - 1 + 1); 
	return throwResult;						 
}
int main()
{			
	int dieTot = 0,
		throwNumber = 0,
		numberSides = 0,
		throwResult = 0;
	int die1 =4;
	int die2 =6;
	int die3 =8;
	int die4 =10;
	int die5 =12;
	int die6 =20;

	char rollAgain;
	srand(unsigned(time(0) ));					   
	cout << "Do you want Play? ";
	cin >> rollAgain;
	do{
		cout << "How many sides? " << endl;
		cout << "1 - 4 sided die\n";
		cout << "2 - 6 sided die\n";
		cout << "3 - 8 sided die\n";
		cout << "4 - 10 sided die\n";
		cout << "5 - 12 sided die\n";
		cout << "6 - 20 sided die\n";

		int choice;
		cout << "Enter choice: ";
		cin >> choice;

	switch( choice )
	{
case 1:
	do
	{
	cout << "You have choosen a 4 sided die? " << endl;
		int numberSides = die1;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

case 2:
	do
	{
		cout << "You have choosen a 6 sided die? " << endl;
		int numberSides = die2;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

case 3:
		do
	{
		cout << "You have choosen a 8 sided die? " << endl;
		int numberSides = die3;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

case 4:
		do
	{
		cout << "You have choosen a 10 sided die? " << endl;
		int numberSides = die4;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

case 5:
	do
	{
		cout << "You have choosen a 12 sided die? " << endl;
		int numberSides = die5;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

case 6:
	do
	{
		cout << "You have choosen a 20 sided die? " << endl;
		int numberSides = die6;
		cin >> numberSides;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		} while (choice != numberSides );
		break;

		default:
			cout << "Not a proper entry.\n";
			break;
	}
	cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;

	return 0;
}

OK so now i can get the program to out put with no errors but it will only work for the first input after that it is all the same output regardless of 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include <iostream>		
#include <ctime>		
using namespace std;
	

int throwDie(int Sides, int &throwResult)
{
	throwResult = 1 + rand() % (Sides - 1 + 1); 
	return throwResult;						 
}
int main()
{			
	int dieTot = 0,
		throwNumber = 0,
		numberSides = 0,
		throwResult = 0;
	int die1 =4;
	int die2 =6;
	int die3 =8;
	int die4 =10;
	int die5 =12;
	int die6 =20;

	char rollAgain;
	srand(unsigned(time(0) ));					   
	cout << "Do you want Play? ";
	cin >> rollAgain;
	
		cout << "How many sides? " << endl;
		cout << "1 - 4 sided die\n";
		cout << "2 - 6 sided die\n";
		cout << "3 - 8 sided die\n";
		cout << "4 - 10 sided die\n";
		cout << "5 - 12 sided die\n";
		cout << "6 - 20 sided die\n";

		int choice;
		cout << "Enter choice: ";
		cin >> choice;

	switch( choice )
	{
case 1:
	do
	{
	cout << "You have choosen a 4 sided die? " << endl;
		int numberSides = die1;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		
	} while (choice != numberSides );
		break;
	
case 2:
	do
	{
		cout << "You have choosen a 6 sided die? " << endl;
		int numberSides = die2;
		
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		} while (choice != numberSides );
		break;

case 3:
		do
	{
		cout << "You have choosen a 8 sided die? " << endl;
		int numberSides = die3;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		} while (choice != numberSides );
		break;

case 4:
		do
	{
		cout << "You have choosen a 10 sided die? " << endl;
		int numberSides = die4;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		} while (choice != numberSides );
		break;

case 5:
	do
	{
		cout << "You have choosen a 12 sided die? " << endl;
		int numberSides = die5;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		} while (choice != numberSides );
		break;

case 6:
	do
	{
		cout << "You have choosen a 20 sided die? " << endl;
		int numberSides = die6;
		cout << numberSides << "-sided die rolled for a value of " << throwDie(numberSides, throwResult) << "!" << endl;
		dieTot = dieTot + throwResult;
		throwNumber++;
		cin >> numberSides;
		}
while (choice != numberSides );
		break;

		default:
			cout << "Not a proper entry.\n";
			break;
	
	cout << endl << "Total for " << throwNumber <<  " throws = " << dieTot << endl;
	
	return 0;
	}}
Topic archived. No new replies allowed.