I wrote this code to re-create the classic race of the tortoise and the hare and everything compiles fine. Just the logic is messed up. When I run the program all I see is BANG AND THEY'RE OFF and who wins. I should be seeing a T for the Tortoise and and H for the Hare going across the screen. Can anyone tell me what I am doing wrong.

``````#include <iostream>
#include <cstdlib>

int main()
{
int i = 0;
int j = 0;
int check;
int check_1;

char play_again = '\n' ;
int tortoise[70];
int hare[70];

cout << "BANG !!!!!\n"
"AND THEY'RE OFF !!!!!" << endl;
while(play_again == '\n')
{
while(i <= 69 && j <= 69)
{
tortoise[i] = 0;;
check = random_1();

if(check == 1 || check == 2 || check == 3 || check == 4 || check ==
5)

tortoise[i+=3] = 0;

if(check == 6 || check == 7)
tortoise[i-=6] = 0;

if(check == 8 || check == 9 || check == 10)
tortoise[i+=1] = 0;

char *ptr;
char line[71] = "----------------------------------------------------------------------";
ptr = line;

if(i < 0){
i = 0;
*(ptr + i) = 'T';}
*(ptr + i) = 'T';
hare[j] = 0;
check_1 = random_1();

if(check_1 == 1 || check_1 == 2)
hare[j] = 0;

if(check_1 == 3 || check_1 == 4)
hare[j+=9] = 0;

if(check_1 == 5)
hare[j+=12] = 0;

if(check_1 == 6 || check_1 == 7 || check_1 == 8)
hare[j++] = 0;

if(check_1 == 9 || check_1 == 10)
hare[j-=2] = 0;

if(j < 0){
j = 0;
*(ptr + j) = 'H';}
*(ptr + j) = 'H';

if(i == j){
*(ptr + i) = 'O';
*(ptr + i + 1) = 'U';
*(ptr + i + 2) = 'C';
*(ptr + i + 3) = 'H';}
break;
}
if(i >= 69)
{
cout << "TORTOISE WINS!!! YAY!!!\n" << endl;
break;
}
if(j >= 69)
{
cout <<"HARE WINS. YUCH.\n" << endl;
break;
}
if(j >= 69 && i >= 69)
{
cout << "IT'S A TIE\n";
break;
}
}
return 0;
}
int random_1()
{
srand(time(NULL));
return (1 + rand() % 10);
}``````

## All 4 Replies

>>char play_again = '\n'
make that = 'y' instead of '\n' because '\n' doesn't make sense

>>while(play_again == '\n')
check for 'y' instead of '\n'

How can you be seeing anything when the code above does not compile?

'\n' is the escape code for newline (what you get when you press the enter key)

'n' is the character 'en'

srand( ) should only be called once in the program, not every time your random_1( ) function is called. If the program runs fast enough, srand will always be resetting to the same start point of the pseudorandom sequence.

Lines like:

``````if(check == 6 || check == 7)
tortoise[i-=6] = 0;``````

What happens when this first runs and the value of i is 0? Or in the cases that add to i occurs near the end of the array?

As to not seeing anything but the start and finish messages, umm, what else is there to see?

The code compiles, but I am suppose to see a T and a H running across the screen. I did the code in C maybe you can help me translate this code into C++ then, because basically thats all I was trying to do in the first play anyways.

``````#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int random_1();

int main()

{

int i = 0;

int j = 0;

int check;

int check_1;

char play_again = '\n' ;

int tortoise[70];

int hare[70];

printf("BANG !!!!!\n");

printf("AND THEY'RE OFF !!!!!\n");

while(play_again == '\n')

{

while(i <= 69 && j <= 69)

{

tortoise[i] = 0;;

check = random_1();

if(check == 1 || check == 2 || check == 3 || check == 4 || check ==
5)

tortoise[i+=3] = 0;

if(check == 6 || check == 7)

tortoise[i-=6] = 0;

if(check == 8 || check == 9 || check == 10)

tortoise[i+=1] = 0;

char *ptr;

char line[71] = "----------------------------------------------------------------------";

ptr = line;

if(i < 0){

i = 0;

*(ptr + i) = 'T';}

*(ptr + i) = 'T';

hare[j] = 0;

check_1 = random_1();

if(check_1 == 1 || check_1 == 2)

hare[j] = 0;

if(check_1 == 3 || check_1 == 4)

hare[j+=9] = 0;

if(check_1 == 5)

hare[j+=12] = 0;

if(check_1 == 6 || check_1 == 7 || check_1 == 8)

hare[j++] = 0;

if(check_1 == 9 || check_1 == 10)

hare[j-=2] = 0;

if(j < 0){

j = 0;

*(ptr + j) = 'H';}

*(ptr + j) = 'H';

if(i == j){

*(ptr + i) = 'O';

*(ptr + i + 1) = 'U';

*(ptr + i + 2) = 'C';

*(ptr + i + 3) = 'H';}

printf("%s\n\n", line);

break;

}

if(i >= 69)

{

printf("TORTOISE WINS!!! YAY!!!\n");

break;

}

if(j >= 69)

{

printf("HARE WINS. YUCH.\n");

break;

}

if(j >= 69 && i >= 69)

{

printf("IT'S A TIE\n");

break;

}

printf("Press enter");

scanf("%c", &play_again);

}

return 0;

}

int random_1()

{

srand(time(NULL));

return (1 + rand() % 10);

}
``````

In what compiler does your first posting compile? I had to fix several things.

To see the Hs and Ts, what's different from your C version and your C++ version?

And the C version is as likely to crash on array violations. Modifying the index value as you're using it to access the array is asking for trouble, and delivering regularly in this program.

And, a little indenting helps readability a whole lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.