S
Shock
Hi everybody,
I have been getting so much good advice here that I figured I would
post another question. I am still learning c++ from a text that I
inherited from my brother. I am now working on a problem that seems
lame, but I really want it to work. It is a tortoise & hare sim. I
am pretty sure I have everything right except printing the array. I
cannot figure out how to print the array correctly. It should print
the T & H one after another depending on the calculated location. I
am having trouble understanding how to input data to the array and
then print it without printing the entire array. Here is my source so
far.
#include <iostream>
#include <ctime>
using std::cout;
using std::endl;
void moveTortoise( int *);
void moveHare( int * );
void printCurrentPositions( int *, int * );
void delay ( double );
char arrayRace[71] = {' '};
int main()
{
int hPos = 1; // hare's position
int tPos = 1; // tortoise's position
cout << "BANG !!!!!" << endl << "AND THEY\'RE OFF !!!!!" << endl;
srand(time(0));
while ( hPos < 70 && tPos < 70 ) {
moveTortoise( &tPos ); //works fine
delay (.2);
moveHare ( &hPos );
printCurrentPositions( &tPos, &hPos ); // problem here!
} // end while
if ( hPos > tPos )
cout << "\nHare wins. Yuch.";
else if ( hPos < tPos )
cout << "\nTORTOISE WINS!!! YAY!!!";
else if ( hPos == tPos )
cout << "\nIts a tie. Good job Tortoise!";
return 0;
} // end main
void moveTortoise( int *tPosptr )
{
int i = ( 1 + rand() % 10 );
if ( ( i >= 1 ) && ( i <= 5 ) )
*tPosptr += 3;
else if ( ( i >= 6 ) && ( i <= 7 ) )
*tPosptr -= 6;
else if ( ( i >= 8 ) && ( i <= 10 ) )
*tPosptr += 1;
if ( *tPosptr < 1 )
*tPosptr = 1;
} // end moveTortoise
void moveHare( int *hPosptr )
{
int j = ( 1 + rand() % 10 );
if ( ( j >= 1 ) && ( j <= 2 ) )
*hPosptr = *hPosptr;
else if ( ( j >= 3 ) && ( j <= 4 ) )
*hPosptr += 9;
else if ( j == 5 )
*hPosptr -= 12;
else if ( ( j >= 6 ) && ( j <= 8 ) )
*hPosptr += 1;
else if ( ( j >= 9 ) && ( j <= 10) )
*hPosptr -= 2;
if ( *hPosptr < 1 )
*hPosptr = 1;
} // end moveHare
void printCurrentPositions( int *tPosptr, int *hPosptr )
{
for ( int track = 1; track <= 70; track++ ) {
if ( track == *tPosptr ) {
arrayRace[track] = 'T';
}
if ( track == *hPosptr ) {
arrayRace[track] = 'H';
}
if ( track == *hPosptr == *tPosptr ) {
arrayRace[track] = 'O';
}
}
cout << arrayRace;
} // end printCurrentPositions
void delay( double how_long )
{
clock_t target = static_cast< clock_t >( how_long * CLOCKS_PER_SEC )
+ clock();
while ( clock() <= target )
{ } // Do Nothing
}
thanks in advance,
shock
I have been getting so much good advice here that I figured I would
post another question. I am still learning c++ from a text that I
inherited from my brother. I am now working on a problem that seems
lame, but I really want it to work. It is a tortoise & hare sim. I
am pretty sure I have everything right except printing the array. I
cannot figure out how to print the array correctly. It should print
the T & H one after another depending on the calculated location. I
am having trouble understanding how to input data to the array and
then print it without printing the entire array. Here is my source so
far.
#include <iostream>
#include <ctime>
using std::cout;
using std::endl;
void moveTortoise( int *);
void moveHare( int * );
void printCurrentPositions( int *, int * );
void delay ( double );
char arrayRace[71] = {' '};
int main()
{
int hPos = 1; // hare's position
int tPos = 1; // tortoise's position
cout << "BANG !!!!!" << endl << "AND THEY\'RE OFF !!!!!" << endl;
srand(time(0));
while ( hPos < 70 && tPos < 70 ) {
moveTortoise( &tPos ); //works fine
delay (.2);
moveHare ( &hPos );
printCurrentPositions( &tPos, &hPos ); // problem here!
} // end while
if ( hPos > tPos )
cout << "\nHare wins. Yuch.";
else if ( hPos < tPos )
cout << "\nTORTOISE WINS!!! YAY!!!";
else if ( hPos == tPos )
cout << "\nIts a tie. Good job Tortoise!";
return 0;
} // end main
void moveTortoise( int *tPosptr )
{
int i = ( 1 + rand() % 10 );
if ( ( i >= 1 ) && ( i <= 5 ) )
*tPosptr += 3;
else if ( ( i >= 6 ) && ( i <= 7 ) )
*tPosptr -= 6;
else if ( ( i >= 8 ) && ( i <= 10 ) )
*tPosptr += 1;
if ( *tPosptr < 1 )
*tPosptr = 1;
} // end moveTortoise
void moveHare( int *hPosptr )
{
int j = ( 1 + rand() % 10 );
if ( ( j >= 1 ) && ( j <= 2 ) )
*hPosptr = *hPosptr;
else if ( ( j >= 3 ) && ( j <= 4 ) )
*hPosptr += 9;
else if ( j == 5 )
*hPosptr -= 12;
else if ( ( j >= 6 ) && ( j <= 8 ) )
*hPosptr += 1;
else if ( ( j >= 9 ) && ( j <= 10) )
*hPosptr -= 2;
if ( *hPosptr < 1 )
*hPosptr = 1;
} // end moveHare
void printCurrentPositions( int *tPosptr, int *hPosptr )
{
for ( int track = 1; track <= 70; track++ ) {
if ( track == *tPosptr ) {
arrayRace[track] = 'T';
}
if ( track == *hPosptr ) {
arrayRace[track] = 'H';
}
if ( track == *hPosptr == *tPosptr ) {
arrayRace[track] = 'O';
}
}
cout << arrayRace;
} // end printCurrentPositions
void delay( double how_long )
{
clock_t target = static_cast< clock_t >( how_long * CLOCKS_PER_SEC )
+ clock();
while ( clock() <= target )
{ } // Do Nothing
}
thanks in advance,
shock