Thread: Void Function not working.. : / (noob coder)

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    20

    Void Function not working.. : / (noob coder)

    Hi guys.. So I'm solving Problem 3 of Chapter 7 in Jumping into C++. Basically it's telling me to create a two player tic tac toe game. As you can see the code is far from finish and I probably will run into a ton of other problems later, but first I want to know why my function for background_work doesn't work. It won't even compile. : /

    I have tested the code without the void function being called and it works fine if put in main. Also I tried declaring it's variables as global variables but it still didn't work (so i just put it back down to keep the code neater). Any help will be appreciated.

    Code:
    // Write a two-player tic-tac-toe game, allowing two humans to play against each other; use enums when possible to represent the values of the board.
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int x; // My clasic and favorite input variable
    void Show_Board ();
    void BackGround_Work ();
    void UserInput ();
    
    
    //Board Spaces as Global Variables
    char Space_1 = ('1');
    char Space_2 = ('2');
    char Space_3 = ('3');
    char Space_4 = ('4');
    char Space_5 = ('5');
    char Space_6 = ('6');
    char Space_7 = ('7');
    char Space_8 = ('8');
    char Space_9 = ('9');
    
    
    int main ()
    {
        
        // Intro and Intstructions...
        cout << "Awesome Student Presents C++ Tic Tac Toe!!\n";
        cout << "Intructions:\n";
        cout << "Get 3 in a row to win on a 3x3 grid!\n";
        cout << "Choose from squares 1 to 9 by inputing a number!\n\n";
        
        cout << "The grid can be seen as followed..\n\n";
        Show_Board();
        
        cout << "Player 1 --> X\n";
        cout << "Player 2 --> O\n\n";
        
        // Game
        for (int i = 0; i < 9; i++)
        {
            cout << "Player 1's Turn!\n";
            UserInput();
            BackGround_Work();
        }
    }
    
    
    
    
    void Show_Board () // Shows the Tic Tac Toe Board
    {
        cout << "\t"<<Space_1<<"\t|\t"<<Space_2<<"\t|\t"<<Space_3<<"\n";
        cout << "--------+-------+-------\n";
        cout << "\t"<<Space_4<<"\t|\t"<<Space_5<<"\t|\t"<<Space_6<<"\n";
        cout << "--------+-------+-------\n";
        cout << "\t"<<Space_7<<"\t|\t"<<Space_8<<"\t|\t"<<Space_9<<"\n\n";
    }
    
    
    void Background_Work ()
    {
        // Variables Declared
        enum TicTacToe {Empty, X_Filled, O_Filled};
        
        int iSpace_1 = Empty;
        int iSpace_2 = Empty;
        int iSpace_3 = Empty;
        int iSpace_4 = Empty;
        int iSpace_5 = Empty;
        int iSpace_6 = Empty;
        int iSpace_7 = Empty;
        int iSpace_8 = Empty;
        int iSpace_9 = Empty;
        
        // Visual Change & Background Work
        bool done = false;
        while (!done) {
            switch (x) {
                case 1:
                    if (iSpace_1 == Empty)
                        Space_1 = ('X'), iSpace_1 = X_Filled;
                    break;
                case 2:
                    if (iSpace_2 == Empty)
                        Space_2 = ('X'), iSpace_2 = X_Filled;
                    break;
                case 3:
                    if (iSpace_3 == Empty)
                        Space_3 = ('X'), iSpace_3 = X_Filled;
                    break;
                case 4:
                    if (iSpace_4 == Empty)
                        Space_4 = ('X'), iSpace_4 = X_Filled;
                    break;
                case 5:
                    if (iSpace_5 == Empty)
                        Space_5 = ('X'), iSpace_5 = X_Filled;
                    break;
                case 6:
                    if (iSpace_6 == Empty)
                        Space_6 = ('X'), iSpace_6 = X_Filled;
                    break;
                case 7:
                    if (iSpace_7 == Empty)
                        Space_7 = ('X'), iSpace_7 = X_Filled;
                    break;
                case 8:
                    if (iSpace_8 == Empty)
                        Space_8 = ('X'), iSpace_8 = X_Filled;
                    break;
                case 9:
                    if (iSpace_9 == Empty)
                        Space_9 = ('X'), iSpace_9 = X_Filled;
                    break;
                default:
                    cout << "Invalid Move!\n";
                    break;
            }
        }
    }
    
    
    void UserInput ()  // So that letter inputs don't mess up the loops!
    {
        while (!(cin >> x))
        {
            cout << "A number please..\n";
            cin.clear();
            cin.ignore(100, '\n');
        }
    }
    Last edited by Jonathan002; 10-26-2013 at 02:07 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    C++ is case sensitive.

    Your code declares and calls a function named BackGround_Work(). Note that the 'G' is upper case. The function implemented is named Background_Work(). Note that the 'g' is lower case. BackGround_Work() and Background_Work() are not interchangeable.


    Also, the code compiles fine (in the sense that the compiler will not pick up syntax errors). The problem is with linking ..... as calling a function that doesn't exist prevents the linker producing an executable file.

    The compiler (which reads source code, and produces object files) and linker (which reads object files and libraries, and produces an executable file) are distinct phases of building a program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    20
    Quote Originally Posted by grumpy View Post
    C++ is case sensitive.

    Your code declares and calls a function named BackGround_Work(). Note that the 'G' is upper case. The function implemented is named Background_Work(). Note that the 'g' is lower case. BackGround_Work() and Background_Work() are not interchangeable.


    Also, the code compiles fine (in the sense that the compiler will not pick up syntax errors). The problem is with linking ..... as calling a function that doesn't exist prevents the linker producing an executable file.

    The compiler (which reads source code, and produces object files) and linker (which reads object files and libraries, and produces an executable file) are distinct phases of building a program.
    Hi Grumpy. Thanks for your time and feedback! I had switched it and it compiled just fine.. : )

    I feel so dumb not noticing this.. >< I just have to train myself to be sharper with noticing case sensitive mistakes like this. Anyway I guess this solves the issue with this thread and now I will continue on with trying to solve this problem. Will be posing a new thread to this problem if I still can't crack it after giving it a solid try effort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with bar graph.. : / (Noob coder)
    By Jonathan002 in forum C++ Programming
    Replies: 15
    Last Post: 10-13-2013, 04:52 AM
  2. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  3. working with void ** (NOOB)
    By MishaMish in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2011, 05:11 PM
  4. Noob - Hello World Not Working
    By icu222much in forum C++ Programming
    Replies: 19
    Last Post: 09-14-2009, 01:18 PM
  5. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM