Not sure what I'm doing wrong (Random Number Generator)

First let me say thanks for this forum. I'm taking my first C++ class (an online class, mind you) and being able to find topics on here has been big help.

I'm in the middle of a project and I cannot figure out what is wrong with my random number generator. Everything else is working but this is giving me fits. It either returns 0 or returns something like "00A5246." Is there something wrong with the generator? Am I not returning it to main correctly? I'm lost at this point. Also, while building the various functions I was inputting a value for int W. It's not there right now so you will need to add it if you want to test the functions without the generator.

Lastly, I tried testing the generator by itself and was getting results outside my max. How is that possible?

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Description:		Shape Display Project
// IDE used:		Visual Studio 2013 Express

#include <iostream>
#include <ctime>
#include <cstdlib>



using namespace std;

int Sel, W;

void displayMenu();
//Displays menu for user to make selection

int getWidth();
/*Calculates random odd number between 3 & 11 to be used for width
in later "display" functions*/

void displaySquare();
//Displays square shape

void displayTriangle();
//Displays triangle shape

void displayUpsideDownTriangle();
//Displays upside down triangle

void displayDiamond();
//Displays diamond shape

int main()
{
	do
	{

		displayMenu();

		cout << "     ";
		cin >> Sel;

		if (Sel == 1 || Sel == 2 || Sel == 3 || Sel == 4)
		{
			getWidth();
		}

		switch (Sel)
		{
		case 1:
			displaySquare();
			break;
		case 2:
			displayTriangle();
			break;
		case 3:
			displayUpsideDownTriangle();
			break;
		case 4:
			displayDiamond();
			break;
		case -9:
			cout << "\n\n     End of Program\n\n";
			break;
		default:
			cout << "\n\n	Invalid selection. Please choose again.\n\n";
		}

	} while (Sel != -9);

	system("pause");
	return 0;

}

void displayMenu()
//Displays menu for user selection
{
	using namespace std;

	cout << endl;
	cout << "     Shape Display Menu\n\n";
	cout << "       1....Square\n";
	cout << "       2....Triangle\n";
	cout << "       3....Upside Down Triangle\n";
	cout << "       4....Diamond\n\n";
	cout << "      -9....Exit Program\n\n";
	cout << "     Make a selection\n\n";

}


int getWidth()
//Generates random odd width between 3 & 11
{
	srand((unsigned)time(0));

	W = 3+(rand()%11);

	while (W == 4 || W == 6 || W == 8 || W == 10 || W == 12 || W == 13)
		{
			W = 3+(rand()%11);
		}

return(W);

}


void displaySquare()
//Displays square shape
{
	int hgt = W;
	
	for (int disY = 0; disY < hgt; disY++)
	{
		cout << "     ";
		for (int disX = 0; disX < W; disX++)
		{
			cout << "#";
		}
		cout << endl;
	}

	cout << endl;

}



void displayUpsideDownTriangle()
//Displays upside down triangle shape
{

	int hgt = W / 2 + 1;
	for (int r = 0; r < hgt; r++)
	{
		cout << "   ";
		for (int x = 0; x < r; x++)
			cout << " ";

		for (int c = 0; c < W; c++)
			cout << "#";

		cout << endl;
		W = W - 2;

	}
}

void displayTriangle()
//Displays triangle shape
{
	
	int Hgt = (W / 2) + 1;
	int dis = W / W;
	int Spc = Hgt;

	for (int x = 0; x < Hgt; x++)
	{
		cout << "   ";
		for (int y = 0; y < (Spc - 1); y++)
		{
			cout << " ";
		}

		for (int z = 0; z < dis; z++)
		{
			cout << "#";
		}
		cout << endl;

		dis = dis + 2;
		Spc = Spc - 1;
	}
}

void displayDiamond()
//Displays diamond shape
{
	displayTriangle();
	displayUpsideDownTriangle();
}

You should only call srand() once at the start of your program, and then never again.

By the way, rand() and srand() are being deprecated - don't use them anymore. Instead, use the new random generators available in the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
Thanks! That helped. Unfortunately, I don't have a choice of what commands to use right now, although I will make note of it for my own use. This professor is very much "if it isn't exactly what I told you, it's wrong" and that's what I was told to use.

Thanks for the help!
Last edited on
Topic archived. No new replies allowed.