dice roller switch statement issue

I made a dice roller, and there is one problem left to fix. When I enter an invalid choice for dieType, it continues on to the next block of code, and will loop infinitely. Let me know if I should post the header and the main.



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

#include "dice.h"
#include <ctime>
#include <cstdlib>

rollDice::rollDice()
{
}

int rollDice::getDieType()
{
	cout << "\n\nTypes of die:\n";
    cout << "1 - d4\n";
    cout << "2 - d6\n";
    cout << "3 - d8\n";
	cout << "4 - d10\n";
	cout << "5 - d12\n";
	cout << "6 - d20\n";

    cout << "Choose a type of die: ";
    cin >> dieType;
	return (dieType);
}

int rollDice::getNumberDie(int dieType)
{

	switch( dieType )
	{
		case 1:
			cout << "How many d4 will you roll? ";
			cin >> howMany;
			break;
		case 2:
			cout << "How many d6 will you roll? ";
			cin >> howMany;
			break;
		case 3:
			cout << "How many d8 will you roll? ";
			cin >> howMany;
			break;
		case 4:
			cout << "How many d10 will you roll? ";
			cin >> howMany;
			break;
		case 5:
			cout << "How many d12 will you roll? ";
			cin >> howMany;
			break;
		case 6:
			cout << "How many d20 will you roll? ";
			cin >> howMany;
			break;
		default:
			cout << "Not a proper entry.\n";
			break;
	}

			return 0;
}


    int rollDice::getNumberRolls()
    {
        cout << "How many times will you roll? ";
        cin >> rolls;
        return (rolls);}

int rollDice::printResult()
{
	for (int i = 1; i <= rolls; i++)
	{
			for (int j = 1; j <= howMany; j++)
		{

			int randomNumber = rand();
				if (dieType == 1)
					die = (randomNumber % 4) + 1;
				else
					if (dieType == 2)
					die = (randomNumber % 6) + 1;
				else
					if (dieType == 3)
					die = (randomNumber % 8) + 1;
				else
					if (dieType == 4)
					die = (randomNumber % 10) + 1;
				else
					if (dieType == 5)
					die = (randomNumber % 12) + 1;
				else
					if (dieType == 6)
					die = (randomNumber % 20) + 1;

			cout << die << "--" ;
			}
			cout << endl;
		}
return 0;
Last edited on
Post wherever you call getNumberDie.
here's the header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
#ifndef dice_h
#define dice_h
#include <cstdlib>

class rollDice
{
public:
	int die;
	int dieType;
	int howMany;
	int rolls;
	rollDice();
	int getDieType();
	int getNumberDie(int);
	int getNumberRolls();
	int printResult();
};

#endif 


and here's the main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

#include "dice.h"
#include <ctime>
#include <cstdlib>

int dieType = 1;
int main()
{
    while (dieType != 9) {
	srand (time(NULL));
	rollDice s;
	s.getNumberDie(s.getDieType());
	s.getNumberRolls();
	s.printResult();
    }
	return 0;
}
bump.
Topic archived. No new replies allowed.