Issues with Using Classes

I'm trying to piece together a program that simulates a race. There are a few different events that could happen each 'round,' so I have a random number that needs to be updated every round (and while loops to determine what happens in the case of each).
I'm aiming for one round per second (and therefore, a new random number every second).

I am getting frequent error messages to the effect of "Unexpected unqualified id before 'while'." I've tried to fix this by putting the while loops in public classes, but to no avail.

I know I can't just do things, and that the loops need to be in a function of some sort, but as I'm still not fluent in C++, I'm having an issue fixing that issue. How can I go about that?

Should it help, the error is occurring in Dev C++.

My code in its current state is below (extraneous bits like the victory conditions have been left out).

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

int main()
{
	int tortoisePosition, harePosition = 1;	
	int currentMove, i;
	
	for(i=0; harePosition && tortoisePosition < 70; i++)
	{
	srand(time(0));
	
	int currentMove = (rand () % 10) + 1; //Random variable to be updated with each pass.
	cout << currentMove << endl;
	}
	system("pause");
}

	//Classes that determine how the tortoise and hare move at each interval.

//=========================Source of main problem below=======================	
	class tortoise() 
	{
		while (currentMove <= 5) //Fast Plod
		{
			cout << "The Tortoise plods quickly!";
			tortoisePosition += 3;
			currentMove = (rand () % 10) + 1;
			return tortoisePosition,  currentMove;
		}
		
		while(6 <= currentMove <= 7) //Slip
		{
			cout << "The Tortoise slips!";
			
			
			tortoisePosition -= 6;
			currentMove = (rand () % 10) + 1;
			return tortoisePosition,  currentMove;
		}
		
		while(currentMove > 7) //Slow plod
		{
			cout << "The Tortoise plods slowly!";
			tortoisePosition += 1;
			currentMove = (rand () % 10) + 1;
			return tortoisePosition,  currentMove;
		}
		
	
	public hare()
	{	
		while (currentMove < 3) // Sleep
		{
			cout << "The Hare takes a nap.";
		}
		
		while (3 <= currentMove <= 4) //big hop
		{
			cout << "The hare takes a long hop!";
			harePosition += 9;
			int currentMove = (rand () % 10) + 1;
			return harePosition, currentMove;
		}
		
		while (currentMove == 5)
		{
			cout << "The hare takes a nasty fall!";
			harePosition -= 12;
			int currentMove = (rand () % 10) + 1;
			return harePosition, currentMove;
		}
		
		while (6 <= currentMove <= 8)
		{
			cout << "The hare takes a small hop!";
			harePosition += 1;
			int currentMove = (rand () % 10) + 1;
			return harePosition, currentMove;
		}
		
		while (currentMove > 8)
		{
			"The hare takes a small slip!";
			harePosition -= 1;
			int currentMove = (rand () % 10) + 1;
			return harePosition, currentMove;
		}
	}  
Last edited on
You need to brush up on how to write a class.
http://www.cplusplus.com/doc/tutorial/classes/
Also I think srand(time(0)); should only be called at the beginning of the program not placed within the for loop.
1
2
3
4
5
6
7
	srand(time(0));  // seed the random number generator so each program run is different.
	
	for(i=0; harePosition && tortoisePosition < 70; i++)
	{
	int currentMove = (rand () % 10) + 1; //Random variable to be updated with each pass.
	cout << currentMove << endl;
	}


Topic archived. No new replies allowed.