Why is this an infinite loop...?

I hope this isn't too much code for anyone to skim through, but basically the first and second function are to move the position of the 'tortoise' and the 'hare', and the third function is printing their relative positions.

The main function just starts the variables and then calls the functions.

Why am I not getting anything but the condition that their positions are the same? i.e. "OUCH!!" is displayed every line.

According to my professor, when I use the line

srand(4);
I should see..
http://www.cs.niu.edu/~mcmahon/cs240/Assign/output8a.html



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
#include<iostream>
#include<cmath>
#include <iomanip>
#include <fstream>

using namespace std;

int RACE_END = 70;


void moveTortoise(int& tortoisePtr)
{
int x;

x = 1 + rand() % 10;

if ( x > 1 and x < 5)
tortoisePtr+=3;
else if ( x = 6 )
tortoisePtr-=6;
else if ( x = 7 )
tortoisePtr-=6;
else 
tortoisePtr+=1;

if ( tortoisePtr < 1 ) 
tortoisePtr = 1;
else if ( tortoisePtr > RACE_END )
tortoisePtr = RACE_END;
else
tortoisePtr+=0;



}

void moveHare(int& harePtr)
{
int x;

x = 1 + rand() % 10;

if ( x > 1 and x < 2)
harePtr+=9;
else if ( x = 3 )
harePtr-=12;
else if ( x > 4 and x < 6 )
harePtr+=1;
else if ( x = 7 )
harePtr-=2;
else if ( x = 8 )
harePtr-=2;
else 
harePtr+=0;

if ( harePtr < 1 ) 
harePtr = 1;
else if ( harePtr > RACE_END )
harePtr = RACE_END;
else
harePtr+=0;



}


void printCurrentPositions(int tortoisePosition, int harePosition)
{
if (harePosition = tortoisePosition)
   cout << setw(harePosition) << "OUCH!!!";
else if (harePosition < tortoisePosition)
   cout << setw(harePosition) << "H"
        << setw(tortoisePosition - harePosition) << "T";
else
{
   cout << setw(tortoisePosition) << "T"
        << setw(harePosition - tortoisePosition) << "H";
}

cout << endl;

}

int main()
{
int tortoisePosition = 1;
int harePosition = 1;
int timer = 0;

srand(4);

cout << "ON YOUR MARK, GET SET..." << endl << "BANG!!!" << endl << "AND THEY'RE OFF!!!" << endl;

while (tortoisePosition != RACE_END and harePosition != RACE_END)
   {
   moveTortoise(tortoisePosition);
   moveHare(harePosition);
   printCurrentPositions(tortoisePosition, harePosition);
   
   timer++;
   }


}
On line 95..."and" is not an operator, the logical operators in C++ are:

&& - and
|| - or
== - equal to
!= - not equal to

You also used "=" instead of "==" inside your if statments.

Just out of curiosity, did you compile this? Your compiler might give you warnings for the "=" and an error for the "and"...
Yes, I did compile it in Quincy 2005. I received no such warnings.

That worked for it correctly going through a loop, but mine is now displaying wrong. For the racer that is behind, they are constantly displayed on the far left, instead of how my instructor's sample output is.

Thanks for that fix though!
I don't really know what Quincy is...but I would still suggest using && instead of "and". Anyway, I think that is resulting from the "setw()" you are using...your code looks correct though...try using a for loop to display the correct number of spaces and see what happens then.
On line 95..."and" is not an operator

Wrong. 'and' is a synonym for '&&'
Wow, seriously? Never knew that >.> Can you use 'or' and 'not' too?
These are all "alternative representations" for keywords (from [lex.key]):
and not and_eq bitand bitor compl not_eq or or_eq xor xor_eq
Surely what you want instead of 'and' (or '&&' as is better known by most compilers) is the logical 'or' (or '||' as is known by most compilers). Otherwise it will always wait until both contestants finish the race and there will be no winner...
Topic archived. No new replies allowed.