0

I know you guys have probably seen a tortoise and hare race like this before but I am trying to do it differently... I am new to C++, the only errors in this are when i call the three methods in the while loop in main, it says identifier not found and i can't figure out why. Thanks a lot if you help me figure this out.

1>c:\users\nick\downloads\tortoisenhare.cpp(33): error C3861: 'moveTort': identifier not found
1>c:\users\nick\downloads\tortoisenhare.cpp(34): error C3861: 'moveHare': identifier not found
1>c:\users\nick\downloads\tortoisenhare.cpp(35): error C3861: 'printRaceLine': identifier not found

// Nick Elliott
// Started on: 2/17/11



#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;


int main ()
{	
	int runCount = 0;
	int tortWins = 0;
	int hareWins = 0;
	int hareWon = 0;
	int tortWon = 0;
	srand ( time(NULL) );
	cout << "The Tortise and the Hare Race!" << endl;
	while(tortWins <= 20 || hareWins <=20)
	{
		cout << "\nBANG !!! AND THEY'RE OFF !!!" << endl;
		int tortPos = 1;
		int harePos = 1;
		int finish = 70;
		int tortMove = 0;
		int hareMove = 0;
		int raceFinished = 0;

		while(tortPos <= 70 && harePos <= 70)
		{
			tortPos = moveTort(tortMove, tortPos, tortWins, tortWon, raceFinished);
			harePos = moveHare(hareMove, harePos, hareWins, raceFinished, hareWon);
			printRaceLine(harePos, tortPos);			
		}
	}
		if(tortWon == 1 && hareWon == 0)
		{
			cout << "\nTORTISE WINS!!! YAY!!!";
		}
		else if(hareWon = 1 && tortWon == 0)
		{
			cout << "Hare wins. Yuch.";
		}
		else if(hareWon == 1 && tortWon == 1)
		{
			cout << "It's a tie";
		}


	cout << "Races Won: " << "Tortise: " << tortWins << "		Hare: " << hareWins;
	return 0;
}
int moveTort(int tortMove, int tortPos, int tortWins, int tortWon, int raceFinished)
{
					tortMove = rand() % 100 + 1;
					if(tortMove >= 1 && tortMove <= 25)
					{
						tortPos = tortPos + 4;
					}
					else if(tortMove >= 26 && tortMove <= 40)
					{
						tortPos = tortPos - 2;
					}
					else if(tortMove >= 41 && tortMove <= 100)
					{
						tortPos = tortPos + 2;
					}
					else if(tortPos < 1)
					{
						tortPos = 1;
					}
					else if(tortPos >= 70)
					{
						tortWins++;
						raceFinished = 1;
						tortWon = 1;
					}
					return tortPos;
}
int moveHare(int hareMove, int harePos, int hareWins, int raceFinished, int hareWon)
{
					hareMove = rand() % 100 + 1;
					if(hareMove >= 1 && hareMove <= 30)
					{
						harePos = harePos + 0;
					}
					else if(hareMove >= 31 && hareMove <= 45)
					{
						harePos = harePos + 10;
					}
					else if(hareMove >= 46 && hareMove <= 65)
					{
						harePos = harePos - 15;
					}
					else if(hareMove >= 66 && hareMove <= 75)
					{
						harePos = harePos + 5;
					}
					else if(hareMove >= 76 && hareMove <= 100)
					{
						harePos = harePos - 3;
					}
					if(harePos < 1)
					{
						harePos = 1;
					}
					else if(harePos >= 70)
					{
						hareWins++;
						raceFinished = 1;
						hareWon = 1;
					}
					return harePos;
}
void printRaceLine(int harePos, int tortPos)
{
	int count = 1;
	while(count <= 70)
	{
		if(count == harePos)
		{
			cout << "H";
			count++;
		}
		else if(count == tortPos)
		{
			cout << "T";
			count++;
		}	
		else if(harePos == tortPos)
		{
			cout << "OUCH!!";
			count++;
		}
		else
			cout << "-";
			count++;
	}
	cout << "\n";
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Fbody
1

Because your functions are declared and defined after your main(). They are not "in-scope" and the compiler does not know they exist yet.

To correct this, you need to either move the definitions up in your code so that they are declared/defined before your main() or add function prototypes.

Here is some information on prototypes (scroll down to the "Declaring Functions" section).

This is what you did, it won't work:

#include <iostream>

int main() {
  std::cout << "in main()" << std::endl;          //generic output statement
  someFunction();                                 //call someFunction()
  std::cin.get();
  return 0;
}                                                 //end main()

void someFunction() {                             //define someFunction()
  std::cout << "in someFunction()" << std::endl;  //generic output statement
}                                                 //end someFunction()

This is what you need to do:

#include <iostream>

void someFunction();                              //function prototype for someFunction()

int main() {
  std::cout << "in main()" << std::endl;          //generic output statement
  someFunction();                                 //call someFunction()
  std::cin.get();
  return 0;
}                                                 //end main()

void someFunction() {                             //define someFunction()
  std::cout << "in someFunction()" << std::endl;  //generic output statement
}                                                 //end someFunction()

Notice the statement I added at Line 3.

Edited by Fbody: n/a

Votes + Comments
thanks a lot.... i just moved them and it worked
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.