i have big problem with do-while

hi my problem is that i need to make random number for the picture and random number for the prize without the do while everything is working perfect but then i need to put star_time and the input to repeat so the person can keep inputting prize until the person wins or the time that it is starting in 30 seconds stop. so i do not know but i am having to many problems with the do while because anything is working


#include <iostream> #include "graph1.h" #include <cstdlib> #include <ctime> #include <cmath>

using namespace std;

int main()
{

1
2
//declaring variables[code]
	[code]unsigned int seed;		//declaring seed 

int price = 0; //declaring win char repeat = 'y'; //repeat program int num = 0; //declaring random number int num1 = 0; //declaring random number int start_time = time(0); int current_time = time(0); int remain_time = 0;


//displaying graphics displayGraphics();

do
{

//get the system time and seed the random number generator seed = (unsigned int)clock();

//picking random number num = rand() % 10 + 1; num1 = rand() % 500 + 500;


if (num == 1)
{
displayBMP("chair.bmp", 155, 90); gout << setPos(155, 345) << "Chair" << endg;
}
1
2
3
else if (num == 2)[code]
		{
			[code]displayBMP("couch.bmp", 155, 90);

gout << setPos(155, 345) << "Couch" << endg;
}
else if (num == 3)
{
displayBMP("dodgers.bmp", 155, 90); gout << setPos(155, 345) << "Dodger’s Tickets for 1 week" << endg;
}
else if (num == 4)
{
displayBMP("freezer.bmp", 155, 90); gout << setPos(155, 345) << "Freezer" << endg;
}
else if (num == 5)
{
displayBMP("jukebox.bmp", 155, 90); gout << setPos(155, 345) << "Juke Box full of LPs" << endg;
}
else if (num == 6)
{
displayBMP("lawnmowers.bmp", 155, 90); gout << setPos(155, 345) << "Lawn Mowers" << endg;
}
else if (num == 7)
{
displayBMP("piano.bmp", 155, 90); gout << setPos(155, 345) << "Piano" << endg;
}
else if (num == 8)
{
displayBMP("pooltable.bmp", 155, 90); gout << setPos(155, 345) << "Pool Table" << endg;
}
else if (num == 9)
{
displayBMP("powerTools.bmp", 155, 90); gout << setPos(155, 345) << "Power Tools" << endg;
}
else if (num == 10)
{
displayBMP("recliner.bmp", 155, 90); gout << setPos(155, 345) << "Recliner" << endg;
}


do
{
//user input cout << "Enter value for prize: "; cin >> price; start_time = time(0);

do
{
if ((price < 500) || (price>1000))
{
cout << "Enter a Valid Number between 500 and 1000"; cin >> price;
}

}while ((price<500) || (price>1000));

if (price<num1)
{
gout << setPos (155, 360) << "HIGHER" << endg;
}
else if (price>num1)
{
gout << setPos (155,360) << "LOWER" << endg;
}
else if (price==num1)
{
gout << setPos (155, 360) << "!!!!!!YOU WIN!!!!!!" << endg;
}

current_time = time(0); remain_time = 30 - (current_time - start_time);

gout << setPos(155, 380) << "Time Remaining " << remain_time << " secs" << endg;

}while ((price!=num1) || (remain_time>0));

gout << setPos (155, 360) << "GAME OVER" << endg; gout << setPos (155, 400) << "correct price" << num1 << endg;











//Ask user to run program again cout << "Run program again? (y/n): "; cin >> repeat;

clearGraphics();

}while ( (repeat == 'y') || (repeat == 'Y') );


return 0;
}
Last edited on
Please edit your post and put the code [code]between code tags[/code].
i am new in this what do you mean with that?
I mean that instead of doing this:

int MyVar; //My Code

You should do this:

[code]int MyVar; //My Code[/code]

And then it looks like this:

int MyVar; //My Code

To edit your post, go to the top of it and click the icon that looks like a pencil on paper.
Er... I hate to break this to you but... you're doing it wrong. For that you should have only one pair of code blocks. I admire the time you must have taken to do that, though!

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
#include <iostream> 
#include "graph1.h" 
#include <cstdlib> 
#include <ctime> 
#include <cmath> 

using namespace std;

int main()
{
//declaring variables
	unsigned int seed;		//declaring seed 

int price = 0;	 //declaring win 
char repeat = 'y';	 //repeat program 
int num = 0;	 //declaring random number 
int num1 = 0;	 //declaring random number 
int start_time = time(0); int current_time = time(0); int remain_time = 0;


//displaying graphics 
displayGraphics();

do
{

//get the system time and seed the random number generator 
seed = (unsigned int)clock();

//picking random number 
num = rand() % 10 + 1; num1 = rand() % 500 + 500;


if (num == 1)
{
displayBMP("chair.bmp", 155, 90); gout << setPos(155, 345) << "Chair" << endg;
}
else if (num == 2)[code]
		{
			displayBMP("couch.bmp", 155, 90);

gout << setPos(155, 345) << "Couch" << endg;
}
else if (num == 3)
{
displayBMP("dodgers.bmp", 155, 90); gout << setPos(155, 345) << "Dodger’s Tickets for 1 week" << endg;
}
else if (num == 4)
{
displayBMP("freezer.bmp", 155, 90); gout << setPos(155, 345) << "Freezer" << endg;
}
else if (num == 5)
{
displayBMP("jukebox.bmp", 155, 90); gout << setPos(155, 345) << "Juke Box full of LPs" << endg;
}
else if (num == 6)
{
displayBMP("lawnmowers.bmp", 155, 90); gout << setPos(155, 345) << "Lawn Mowers" << endg;
}
else if (num == 7)
{
displayBMP("piano.bmp", 155, 90); gout << setPos(155, 345) << "Piano" << endg;
}
else if (num == 8)
{
displayBMP("pooltable.bmp", 155, 90); gout << setPos(155, 345) << "Pool Table" << endg;
}
else if (num == 9)
{
displayBMP("powerTools.bmp", 155, 90); gout << setPos(155, 345) << "Power Tools" << endg;
}
else if (num == 10)
{
displayBMP("recliner.bmp", 155, 90); gout << setPos(155, 345) << "Recliner" << endg;
}


do
{
//user input 
cout << "Enter value for prize: "; cin >> price; start_time = time(0);

do
{
if ((price < 500) || (price>1000))
{
cout << "Enter a Valid Number between 500 and 1000"; cin >> price;
}

}while ((price<500) || (price>1000));

if (price<num1)
{
gout << setPos (155, 360) << "HIGHER" << endg;
}
else if (price>num1)
{
gout << setPos (155,360) << "LOWER" << endg;
}
else if (price==num1)
{
gout << setPos (155, 360) << "!!!!!!YOU WIN!!!!!!" << endg;
}

current_time = time(0); remain_time = 30 - (current_time - start_time);

gout << setPos(155, 380) << "Time Remaining " << remain_time << " secs" << endg;

}while ((price!=num1) || (remain_time>0));

gout << setPos (155, 360) << "GAME OVER" << endg; gout << setPos (155, 400) << "correct price" << num1 << endg;


//Ask user to run program again 
cout << "Run program again? (y/n): "; cin >> repeat;

clearGraphics();

}while ( (repeat == 'y') || (repeat == 'Y') );


return 0;
}


Now... can you clarify as to what your problems are? You said you're having problems with the do-while...

-Albatross
Last edited on
thank you and i am sorry about that... my problem is that without the do while the rand function works perfect and pick a random number for both the picture and the prize price, but with the do it always pick 2 for the picture (couch) and 967 for the price... the game should be working until the person guess the answer or the time go from 30 to 0... neither one of them make the loop stop. it start going -1 -2 -3 in the clock and say you win but never exit the program i have been debugging and everything and i cannot find my problems thank you..
Topic archived. No new replies allowed.