Calling a function from a function that comes after it

This is a discussion on Calling a function from a function that comes after it within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to call again() from game(), it works on windows but not on mac. When I call it, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69

    Question Calling a function from a function that comes after it

    Hi, I want to call again() from game(), it works on windows but not on mac. When I call it, it says again() is not defined, so I put it before game. Now game() isn't defined. Is there anyway to make this mac compatible?
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    int again()
    {
        using std::cout;
        using std::cin;
        using std::endl;
        cout << "Play again? Y/N ";
        char again;
        cin >> again;
        if (again == 'y' || again == 'Y')
        {
            game();
        }
        else
        {
        return 0;
        }
    }
    
    int game()
    {
        using std::cout;
        using std::cin;
        using std::endl;
        
        srand((unsigned)time(NULL));
        int randomnum, difficultnumber, number;
        randomnum = rand()%1001;
        char difficulty;
        cout << "Difficulty\n[E]asy (15 guesses) [N]ormal (10 guesses) or [H]ard (5 guesses)?: ";
        cin >> difficulty;
        if (difficulty == 'e' || difficulty == 'E')
        {
            difficultnumber = 15;
            cout << "Easy." << endl << endl << "You have " << difficultnumber << " guesses left.";
        }
        else
        {
            if (difficulty == 'n' || difficulty == 'N')
            {
                difficultnumber = 10;
                cout << "Normal." << endl << endl << "You have " << difficultnumber << " guesses left.";
            }
            else
            {
                if (difficulty == 'h' || difficulty == 'H')
                {
                    difficultnumber = 5;
                    cout << "Hard." << endl << endl << "You have " << difficultnumber << " guesses left.";
                }
            }
        }
        while (difficultnumber > 0)
        {
            cout << "\nGuess a number between 1 and 1000: ";
            cin >> number;
            if (number == randomnum)
            {
                cout << "You won in " << difficultnumber << " guesses!\n";
    			again();
            }
            if (number > randomnum)
            {
                cout << "Too High.";
                difficultnumber--;
                if (difficultnumber == 0)
                {
                    cout << endl << "\nSorry, but you have lost. The number was " << randomnum << ".\n";
    				again();
                }
                else
                {
                    cout << "\nYou have " << difficultnumber << " guesses left.\n";
                }   
            }
            if (number < randomnum)
            {
                cout << "Too Low.";
                difficultnumber--;
                if (difficultnumber == 0)
                {
                    cout << endl << "\nSorry, but you have lost. The number was " << randomnum << ".\n";
    				again();
                }
                else
                {
                    cout << "\nYou have " << difficultnumber << " guesses left.\n";
                }
            }
        }    
    }
    
    int main()
    {
        game()
    	return 0;
    }
    Thank you,
    Adam

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Provide the complete error please. Is all of this in 1 file as your paste suggests?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    From what I see, you need to use function prototypes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    USA
    Posts
    69
    First of all, Error 1: error: `game' undecloare (first use this function)
    Error 2: error: 1int game()' used prior to declaration
    Error 3: error: parse error before `return' // I already fixed this one
    And yes this is all one file.
    And second of all, what are function prototypes?
    Thank you for the speedy response time,
    Adam

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    And second of all, what are function prototypes?
    You might want to read this website's tutorial on functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    function prototypes......
    nextus, the samurai warrior

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21