views:

405

answers:

4

I was having some problems with my first C++ program (this one) awhile ago. Basically I am trying to do an assignment for intro to C++ class where the professor has taught us no syntax. Here is my code right now:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>


using namespace std;
class Race
{
  public:
     void main()
     {

        executeRace();

        int randomMove()
        {
                srand(time(NULL));
                int randomInt = rand() % 100 + 1;
                return randomInt;
        }

        void executeRace()
        {
                int rabbitPosition = 1;
                int turtlePosition = 1;

                cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

                while (rabbitPosition <=70 && turtlePosition <=70)
                {
                        printPositions(rabbitPosition, turtlePosition);

                        turtlePosition = turtleMoveSquares(turtlePosition);
                        rabbitPosition = rabbitMoveSquares(rabbitPosition);
                }

                printWinner(rabbitPosition, turtlePosition);

                tie(rabbitPosition, turtlePosition);
        }

        int turtleMoveSquares(int tPosition)
        {

                int turtleMove = randomMove();

                if(turtleMove >=1 && turtleMove <= 40)
                        tPosition = tPosition + 4;

                if(turtleMove >= 41 && turtleMove <= 50 )
                        tPosition = tPosition - 2;

                if(turtleMove >=51 && turtleMove <=100)
                        tPosition = tPosition + 2;

                if(tPosition < 1)
                        tPosition = 1;

                return tPosition;
        }

        int rabbitMoveSquares(int rabbitPosition)
        {

                int rabbitMove = randomMove();

                if(rabbitMove >=1 && rabbitMove <= 25)
                        rabbitPosition = rabbitPosition;

                if(rabbitMove >=26 && rabbitMove <= 55)
                        rabbitPosition = rabbitPosition + 10;

                if(rabbitMove >=56 && rabbitMove <=60)
                        rabbitPosition = rabbitPosition - 15;

                if(rabbitMove >=61 && rabbitMove <= 90)
                        rabbitPosition = rabbitPosition + 5;

                if(rabbitMove >=90 && rabbitMove <=100)
                        rabbitPosition = rabbitPosition - 3;

                if(rabbitPosition < 1)
                        rabbitPosition = 1;

                return rabbitPosition;
        }

        void printPositions(int rabbitPositions, int turtlePositions)
        {
                int turtleCount;
                int rabbitCount;
                int endCount;

                if(rabbitPositions == turtlePositions && rabbitPositions != 1)
                {
                        turtleCount = 1;

                        while(turtleCount < turtlePositions)
                        {
                                cout << "-";
                                turtleCount = turtleCount+1;
                        }
                        cout << "OUCH!";
                }

                else
                {
                        turtleCount = 1;
                        rabbitCount = 1;
                        endCount=1;

                        if(turtlePositions < rabbitPositions)
                        {
                                while(turtleCount < turtlePositions)
                                {
                                        cout <<  "-";
                                                turtleCount = turtleCount+1;
                                }
                                cout << "T";

                                while(rabbitCount < (rabbitPositions - turtlePositions))
                                {
                                        cout <<  "-";
                                        rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                        }

                        if(rabbitPositions < turtlePositions)
                        {
                                while(rabbitCount < rabbitPositions)
                                {
                                        cout << "-";
                                                rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                                while(turtleCount < (turtlePositions - rabbitPositions))
                                {
                                        cout << "-";
                                        turtleCount = turtleCount+1;
                                }
                                cout << "T";

                                cout << "\n";
                        }
                }
        }

        void printWinner(int rabbitPosition, int turtlePosition)
        {
                if(turtlePosition >= 70 && rabbitPosition < 70)
                {
                        cout << "TORTOISE WINS!!! YAY!!!\n";
                }
                else if(rabbitPosition >=70 && turtlePosition < 70)
                {
                        cout << "Hare wins. Yuch.\n";
                }
                else if(rabbitPosition >=70 && turtlePosition >=70)
                {
                        cout << "It's a tie\n";
                }
        }

        void tie(int turtlePosition, int rabbitPosition)
        {
                if(rabbitPosition >=70 && turtlePosition >=70)
                        executeRace();
        }

    }
};



int main()
{
  Race race;
  race.main();
  return EXIT_SUCCESS;
}

and here are my errors on compile:

uxb3% g++ o- Race Race.cc
g++: o-: No such file or directory
g++: Race: No such file or directory
Race.cc: In member function 'void Race::main()':
Race.cc:14: error: 'executeRace' was not declared in this scope
Race.cc:17: error: a function-definition is not allowed here before '{' token
Race.cc:24: error: a function-definition is not allowed here before '{' token
Race.cc:44: error: a function-definition is not allowed here before '{' token
Race.cc:64: error: a function-definition is not allowed here before '{' token
Race.cc:90: error: a function-definition is not allowed here before '{' token
Race.cc:153: error: a function-definition is not allowed here before '{' token
Race.cc:169: error: a function-definition is not allowed here before '{' token

Sorry to keep bothering you guys about this assignment, but it is my first and I am very, VERY frustrated and obsessed at the moment.

+2  A: 

I think you should be using gcc -o ..., not gcc o- ....

And, secondly, you can't define functions withing functions in C++.

Move the other function definition outside of your class void main(): specifically you need to move the second last brace before your int main() to immediately before int randomMove().

paxdiablo
+8  A: 

You cannot have functions inside your functions*.

You probably want this:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>


using namespace std;
class Race
{
  public:
    int randomMove()
    {
            srand(time(NULL));
            int randomInt = rand() % 100 + 1;
            return randomInt;
    }

    void executeRace()
    {
            int rabbitPosition = 1;
            int turtlePosition = 1;

            cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

            while (rabbitPosition <=70 && turtlePosition <=70)
            {
                    printPositions(rabbitPosition, turtlePosition);

                    turtlePosition = turtleMoveSquares(turtlePosition);
                    rabbitPosition = rabbitMoveSquares(rabbitPosition);
            }

            printWinner(rabbitPosition, turtlePosition);

            tie(rabbitPosition, turtlePosition);
    }

    int turtleMoveSquares(int tPosition)
    {

            int turtleMove = randomMove();

            if(turtleMove >=1 && turtleMove <= 40)
                    tPosition = tPosition + 4;

            if(turtleMove >= 41 && turtleMove <= 50 )
                    tPosition = tPosition - 2;

            if(turtleMove >=51 && turtleMove <=100)
                    tPosition = tPosition + 2;

            if(tPosition < 1)
                    tPosition = 1;

            return tPosition;
    }

    int rabbitMoveSquares(int rabbitPosition)
    {

            int rabbitMove = randomMove();

            if(rabbitMove >=1 && rabbitMove <= 25)
                    rabbitPosition = rabbitPosition;

            if(rabbitMove >=26 && rabbitMove <= 55)
                    rabbitPosition = rabbitPosition + 10;

            if(rabbitMove >=56 && rabbitMove <=60)
                    rabbitPosition = rabbitPosition - 15;

            if(rabbitMove >=61 && rabbitMove <= 90)
                    rabbitPosition = rabbitPosition + 5;

            if(rabbitMove >=90 && rabbitMove <=100)
                    rabbitPosition = rabbitPosition - 3;

            if(rabbitPosition < 1)
                    rabbitPosition = 1;

            return rabbitPosition;
    }

    void printPositions(int rabbitPositions, int turtlePositions)
    {
            int turtleCount;
            int rabbitCount;
            int endCount;

            if(rabbitPositions == turtlePositions && rabbitPositions != 1)
            {
                    turtleCount = 1;

                    while(turtleCount < turtlePositions)
                    {
                            cout << "-";
                            turtleCount = turtleCount+1;
                    }
                    cout << "OUCH!";
            }

            else
            {
                    turtleCount = 1;
                    rabbitCount = 1;
                    endCount=1;

                    if(turtlePositions < rabbitPositions)
                    {
                            while(turtleCount < turtlePositions)
                            {
                                    cout <<  "-";
                                            turtleCount = turtleCount+1;
                            }
                            cout << "T";

                            while(rabbitCount < (rabbitPositions - turtlePositions))
                            {
                                    cout <<  "-";
                                    rabbitCount = rabbitCount+1;
                            }
                            cout << "H";

                    }

                    if(rabbitPositions < turtlePositions)
                    {
                            while(rabbitCount < rabbitPositions)
                            {
                                    cout << "-";
                                            rabbitCount = rabbitCount+1;
                            }
                            cout << "H";

                            while(turtleCount < (turtlePositions - rabbitPositions))
                            {
                                    cout << "-";
                                    turtleCount = turtleCount+1;
                            }
                            cout << "T";

                            cout << "\n";
                    }
            }
    }

    void printWinner(int rabbitPosition, int turtlePosition)
    {
            if(turtlePosition >= 70 && rabbitPosition < 70)
            {
                    cout << "TORTOISE WINS!!! YAY!!!\n";
            }
            else if(rabbitPosition >=70 && turtlePosition < 70)
            {
                    cout << "Hare wins. Yuch.\n";
            }
            else if(rabbitPosition >=70 && turtlePosition >=70)
            {
                    cout << "It's a tie\n";
            }
    }

    void tie(int turtlePosition, int rabbitPosition)
    {
            if(rabbitPosition >=70 && turtlePosition >=70)
                    executeRace();
    }
};

int main()
{
  Race race;
  race.executeRace();
  return EXIT_SUCCESS;
}

*Unless dealing with higher-level languages, such as calculus, of course!

strager
+1 for the link :D
krebstar
+3  A: 

Compiler options:

g++ -o Race Race.cc

You're also declaring functions within your Race::main function

class Race
{
  public:
     void main()
     {

        executeRace();
     } // <----- add this
geofftnz
when I do that I get errors:Race.cc: In member function 'void Race::main()':Race.cc:13: error: expected `;' before ':' tokenRace.cc: At global scope:Race.cc:175: error: expected unqualified-id before '}' tokenRace.cc:175: error: expected declaration before '}' token
fixed 2 errors, but still get:Race.cc: In member function 'void Race::main()':Race.cc:13: error: expected `;' before ':' token
nvm, fixed it. I had a ':' instead of a ';' in one place. Compiled fine. Gonna try and run it now.
yep, the curse of the semicolons. You'll hate them at first, but come to love them. Then you will reach Enlightenment.
geofftnz
+1  A: 

You want to pull out the various functions which are at the moment included in the main() function defined in Race (inside its braces).

Select them, Ctrl-X (Cut), move to above the main() declaration, Ctrl-V (Paste). Reformat. Recompile.

Morendil
Or he can just move the ending brace up. Also, copy/paste are editor specific. =]
strager