srand

I am completely overwhelmed. I have watched youtube videos, sought out examples... No matter what number I enter the turtle always wins with a score of 101. I would be so grateful if someone could let me know what I am doing wrong. Regards,Rheeler

#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <array>
#include <string>
#include <iomanip>
#include<conio.h>

using namespace std;
const int RACE_END = 50;
void moveTortoise(int *);
void moveHare(int *);
void printCurrentPositions(int *, int *);
//const int RAND_MAX=5;


int main()
{

//int roll=rand()%6+1;
//return roll;
//time_t t;
//time(&t);
//srand(t);
int tortoise=1;
int hare =1;
cout << "Enter a number between 1 and 5\n";
_getch();
// int seed2(unsigned int seed);
srand((unsigned)time(0));
int random_number=(rand()%5);

//cout << "Welcome to * Game World. Enjoy your Play" << endl;

//int seed=rand()%5;

//srand(seed);
//cout << seed << endl;


//cout << seed << endl;

//controls race
while (tortoise != RACE_END && hare != RACE_END)
{
moveTortoise(&tortoise);
moveHare(&hare);
printCurrentPositions(&tortoise, &hare);

} //end while


if (tortoise >= hare)
cout << "\nTortoise wins!\n" << tortoise << endl;
else
cout << "\nHare wins!\n" << hare << endl;
system("pause");
return 0;
} //end main

void moveTortoise(int *turtlePtr)
{
int x = rand()%5;
if (int x =1)
*turtlePtr += 4;
else if (int x=2)
*turtlePtr -= 2;
else if (int x = 3)
*turtlePtr += 3;
else if (int x = 4)
*turtlePtr += 4;
else if (int x = 5)
*turtlePtr -= 2;
else if (*turtlePtr > RACE_END)
*turtlePtr=RACE_END;
system("pause");
}//end move tortoise

void moveHare(int *harePtr)
{
int y = rand()%5;
if (int y =1)
*harePtr += 2;
else if (int y = 2)
*harePtr -= 2;
else if (int y = 3)
*harePtr += 4;
else if (int y = 4)
*harePtr -= 3;
else if (int y = 5)
*harePtr += 1;

if (*harePtr <1)
*harePtr=1;
else if (*harePtr > RACE_END)
*harePtr=RACE_END;
} //end move hare


// output positions of animals
/* Write function definition for printCurrentPositions here */
void printCurrentPositions(int *bunnyPtr,int *snapperPtr)
{
if ( *bunnyPtr == *snapperPtr )
cout << setw( *bunnyPtr ) << "OUCH!!!";

else if ( *bunnyPtr < *snapperPtr )
cout << setw( *bunnyPtr ) << "H"
<< setw( *snapperPtr - *bunnyPtr ) << "T";

else
cout << setw( *snapperPtr ) << "T"
<< setw( *bunnyPtr - *snapperPtr ) << "H";

cout << "\n";

} // end function printCurrentPositions
1
2
if (int x =1)
  *turtlePtr += 4;


This is all wrong.

2 things in particular:

1) The "int" there creates a new variable named x (which means you are ignoring the variable you already made, which is not what you want to do)

2) You are using the assignment operator (=) and not the comparison operator (==), so this will always result in "true", and therefore the turtle will always move 4.

You have this problem several times in your code. You'll need to fix all of them.

It should look like this:

1
2
3
4
if (x == 1)  // no "int", use == instead of =
  *turtlePtr += 4;
else if (x == 2)  // same here
  *turtlePtr -= 2;


Also note that rand() % 5 will give you [0..4], not [1..5] as you seem to think.
Damn you Diche. I had this huge reply set up, and just checked to see if anyone else replied. =]

In addition to what Diche told you, I was also going to mention you may want to look into references
http://www.cplusplus.com/doc/tutorial/pointers/


Oh and way to go, it's an interesting program. I won't go into system("pause"); at the moment, but you may want to switch to cin.get(); or something similar! (it's not a big deal if you're just using it on your own computer, but when you get to other's, there's a possibility of renamed console programs and all sorts of things ;)

Last edited on
#include <stdlib.h>
#include <stdio.h>
Thank you so much. Made the changes you suggested. Have tried int random_number=rand()%6 above the main but still get same answer 101. Also tried below main and lose my scope. I am so grateful for your help.

#include <ctime>
#include <iostream>
#include <array>
#include <string>
#include <iomanip>
#include<conio.h>

using namespace std;
const int RACE_END = 50;
void moveTortoise(int *);
void moveHare(int *);
void printCurrentPositions(int *, int *);
//const int RAND_MAX=5;

//int random_number=(rand()%5);
int main()
{

//int roll=rand()%6+1;
//return roll;
//time_t t;
//time(&t);
//srand(t);
int tortoise=1;
int hare =1;
cout << "Enter a number between 1 and 5\n";
_getch();
// int seed2(unsigned int seed);
srand((unsigned)time(0));
int random_number=rand()%6;
cout << random_number << endl;

//cout << "Welcome to * Game World. Enjoy your Play" << endl;

//int seed=rand()%5;

//srand(seed);
//cout << seed << endl;


//cout << seed << endl;

//controls race

while (tortoise != RACE_END && hare != RACE_END)
{
moveTortoise(&tortoise);
moveHare(&hare);
printCurrentPositions(&tortoise, &hare);

} //end while


if (tortoise >= hare)
cout << "\nTortoise wins!\n" << tortoise << endl;
else
cout << "\nHare wins!\n" << hare << endl;
system("pause");
return 0;
} //end main

void moveTortoise(int *turtlePtr)
{
//int x = random_number; // rand()%5;
if (random_number ==1)
*turtlePtr += 4;
else if (random_number==2)
*turtlePtr -= 2;
else if (random_number == 3)
*turtlePtr += 3;
else if (random_number == 4)
*turtlePtr += 4;
else if (random_number== 5)
*turtlePtr -= 2;
else if (*turtlePtr > RACE_END)
*turtlePtr=RACE_END;
system("pause");
}//end move tortoise

void moveHare(int *harePtr)
{
//int y = random_number; //rand()%5;
if (random_number==1)
*harePtr += 2;
else if (random_number== 2)
*harePtr -= 2;
else if (random_number== 3)
*harePtr += 4;
else if (random_number== 4)
*harePtr -= 3;
else if (random_number== 5)
*harePtr += 1;

if (*harePtr <1)
*harePtr=1;
else if (*harePtr > RACE_END)
*harePtr=RACE_END;
} //end move hare


// output positions of animals
/* Write function definition for printCurrentPositions here */
void printCurrentPositions(int *bunnyPtr,int *snapperPtr)
{
if ( *bunnyPtr == *snapperPtr )
cout << setw( *bunnyPtr ) << "OUCH!!!";

else if ( *bunnyPtr < *snapperPtr )
cout << setw( *bunnyPtr ) << "H"
<< setw( *snapperPtr - *bunnyPtr ) << "T";

else
cout << setw( *snapperPtr ) << "T"
<< setw( *bunnyPtr - *snapperPtr ) << "H";

cout << "\n";

} // end function printCurrentPositions
Why did you get rid of 'x'?

Now that you're using a global 'random_number', you only call rand() once, which means they will always move the same amount every time because you use the same number every time.


Also, rand() % 6 gives you [0..5], not [1..5]

hint: start at 0, not 1
Toroise wins at 50 no matter number entered. Reviewed the pointer article. I am sure I'm just missing the forest for the trees. I am so grateful for your help and the opportunity to learn this.
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <iostream>

#include <array>
#include <string>
#include <iomanip>
#include<conio.h>

using namespace std;
const int RACE_END = 50;
void moveTortoise(int *);
void moveHare(int *);
void printCurrentPositions(int *, int *);
//const int RAND_MAX=5;



int main()
{

int tortoise=0;
int hare =0;
cout << "Enter a number between 1 and 5\n";
_getch();
srand((unsigned)time(0));
// int random_number=rand()%6;

//cout << random_number << endl;
//int seed=*random_number;

//cout << "Welcome to * Game World. Enjoy your Play" << endl;

//controls race

while (tortoise != RACE_END && hare != RACE_END)
{
moveTortoise(&tortoise);
moveHare(&hare);
printCurrentPositions(&tortoise, &hare);

} //end while


if (tortoise >= hare)
cout << "\nTortoise wins!\n" << tortoise << endl;
else
cout << "\nHare wins!\n" << hare << endl;
system("pause");
return 0;
} //end main

void moveTortoise(int *turtlePtr)
{
int x = rand()%6;
if (x==1)
*turtlePtr += 4;
else if (x==2)
*turtlePtr -= 2;
else if (x == 3)
*turtlePtr += 3;
else if (x == 4)
*turtlePtr += 4;
else if (x== 5)
*turtlePtr -= 2;
else if (*turtlePtr > RACE_END)
*turtlePtr=RACE_END;
system("pause");
}//end move tortoise

void moveHare(int *harePtr)
{
int y = rand()%6;
if (y==1)
*harePtr += 2;
else if (y== 2)
*harePtr -= 2;
else if (y== 3)
*harePtr += 4;
else if (y== 4)
*harePtr -= 3;
else if (y== 5)
*harePtr += 1;

if (*harePtr <1)
*harePtr=1;
else if (*harePtr > RACE_END)
*harePtr=RACE_END;
} //end move hare


// output positions of animals
/* Write function definition for printCurrentPositions here */
void printCurrentPositions(int *bunnyPtr,int *snapperPtr)
{
if ( *bunnyPtr == *snapperPtr )
cout << setw( *bunnyPtr ) << "OUCH!!!";

else if ( *bunnyPtr < *snapperPtr )
cout << setw( *bunnyPtr ) << "H"
<< setw( *snapperPtr - *bunnyPtr ) << "T";

else
cout << setw( *snapperPtr ) << "T"
<< setw( *bunnyPtr - *snapperPtr ) << "H";

cout << "\n";

} // end function printCurrentPositions
I'll have to try it out when I get home from work
Ok. It is agreed the last question was stupid. Thank you so much. It seems to be working now. Highest regards.
Topic archived. No new replies allowed.