Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By oogabooga
  • 1 Post By Salem

C++ professor requiring 3 files for headers?

This is a discussion on C++ professor requiring 3 files for headers? within the C++ Programming forums, part of the General Programming Boards category; Whenever I wanted to use header files I would just make a .cpp and .h file. However the professor I'm ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    C++ professor requiring 3 files for headers?

    Whenever I wanted to use header files I would just make a .cpp and .h file. However the professor I'm taking wants two .cpp files and a header (one cpp file having the same name name as the header, the other main).

    What is the purpose of the .cpp file with the same name and how do I structure it?

    My code if you need it, doubt you will, thanks!!!!!

    Also would like to add that without you guys life in my C classes would have been hell. You helped me through a lot when I was learning this for fun (I still am, just not on my own anymore :P) and extremely grateful for that. I'll try to beat you guys to posts but you guys are fast!

    main.cpp
    Code:
    #include <iostream>
    using namespace std;
    #include "TicTacToe.h"
    
    int main()
    {
        int turns = 0;
    	TicTacToe game;
    
    	game.outputGrid();
    
    
        for(;;)
        {
            do
            {
                cout << "Player 1's turn (X)" << endl;
                do
                {
                    game.getRow();
                    game.checkRow();
                }while(game.checkRowOK() == false);
    
                do
                {
                    game.getColumn();
                    game.checkColumn();
                }while(game.checkColumnOK() == false);
    
                game.checkTile();
            }while(game.isEmpty() == false);
    
            game.fillTile('X');
            cout << endl;
            game.outputGrid();
    
            game.checkWin();
            if(game.isWin() == true)
            {
                cout << endl << "Player 1 (X) Wins!" << endl;
                break;
            }
    
            game.checkSpaceLeft();
    
            if (game.isSpaceLeft() == false)
            {
                cout << endl << "No more spaces left, it's a tie!" << endl;
                break;
            }
    
    
            turns++;
    
            do
            {
                cout << "Player 2's turn (O)" << endl;
                do
                {
                    game.getRow();
                    game.checkRow();
                }while(game.checkRowOK() == false);
    
                do
                {
                    game.getColumn();
                    game.checkColumn();
                }while(game.checkColumnOK() == false);
    
                game.checkTile();
            }while(game.isEmpty() == false);
    
            game.fillTile('O');
            cout << endl;
            game.outputGrid();
    
            game.checkWin();
            if(game.isWin() == true)
            {
                cout << endl << "Player 2 (O) Wins!" << endl;
                break;
            }
            turns++;
    
        }
    
    	return 0;
    }
    .h
    Code:
    #include <iostream>
    using namespace std;
    
    class TicTacToe
    {
    	private:
    		char Grid[3][3];
    		int inputrow;
    		int inputcolumn;
    		bool RowOK;
    		bool ColumnOK;
    		bool win;
    		bool empty;
            char winningPlayer;
            bool spaceLeft;
    
    	public:
    		TicTacToe();
    		void outputGrid();
    
    		void getRow();
    		void checkRow();
    		bool checkRowOK();
    
            void getColumn();
    		void checkColumn();
    		bool checkColumnOK();
    
            void checkTile();
    		bool isEmpty();
    
    		void fillTile(char);
    
    		void checkWin();
    
    		bool isWin();
    
    		void checkSpaceLeft();
    		bool isSpaceLeft();
    
    };
    
    TicTacToe::TicTacToe()
    {
    	for(int a = 0; a<3; a++)
    		for(int b = 0; b<3; b++)
    			Grid[a][b] = ' '; // space = empty, 1 = X, 2 = O
    
        win = false;
        spaceLeft = true;
    }
    
    void TicTacToe::outputGrid()
    {
    	cout << "  3  4  5" << endl;
    	cout  << " " << (char)201 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)187 << endl;
    	cout  << "0" << (char)186 << Grid[0][0] << " " << (char)179 << Grid[0][1] << " " << (char)179 << Grid[0][2] << " " << (char)186 << endl;
    	cout  << " " << (char)199 << (char)196  << (char)196 << (char)197 << (char)196  << (char)196 << (char)197  << (char)196  << (char)196  << (char)182 << endl;
    	cout  << "1" << (char)186 << Grid[1][0] << " " << (char)179 << Grid[1][1] << " " << (char)179 << Grid[1][2] << " " << (char)186 << endl;
    	cout  << " " << (char)199 << (char)196  << (char)196  << (char)197  << (char)196  << (char)196 << (char)197  << (char)196  << (char)196  << (char)182 << endl;
    	cout  << "2" << (char)186 << Grid[2][0] << " " << (char)179 << Grid[2][1] << " " << (char)179 << Grid[2][2] << " " << (char)186 << endl;
    	cout  << " " << (char)200 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)188 <<"\n\n";
    }
    
    void TicTacToe::getRow()
    {
    	cout << "Input Row (0-2): ";
    	cin >> inputrow;
    }
    
    void TicTacToe::checkRow()
    {
    	if (inputrow < 0 || inputrow > 2)
    	{
    		cout << "Error. Please enter a number between 0 and 2." << endl;
    		RowOK = false;
    	}
    
    	else
    		RowOK = true;
    }
    
    bool TicTacToe::checkRowOK()
    {
        return RowOK;
    }
    
    
    void TicTacToe::getColumn()
    {
    	cout << "Input Column (3-5): ";
    	cin >> inputcolumn;
    }
    
    void TicTacToe::checkColumn()
    {
    	if (inputcolumn < 3 || inputcolumn > 5)
    	{
    		cout << "Error. Please enter a number between 3 and 5." << endl;
    		ColumnOK = false;
    	}
    
    	else
    		ColumnOK = true;
    }
    
    bool TicTacToe::checkColumnOK()
    {
        return ColumnOK;
    }
    
    void TicTacToe::checkTile()
    {
        if(Grid[inputrow][inputcolumn-3] == ' ')
            empty = true;
    
        else
        {
            cout << "Tile is already filled, please try again." << endl;
            empty = false;
        }
    
    }
    
    bool TicTacToe::isEmpty()
    {
        return empty;
    }
    
    void TicTacToe::fillTile(char player)
    {
        Grid[inputrow][inputcolumn-3] = player;
    }
    
    void TicTacToe::checkWin()
    {
        if(Grid[0][0] != ' ' && Grid[0][0] == Grid[0][1] && Grid[0][0] == Grid[0][2] )
        {
            win = true;
        }
    
        if(Grid[1][0] != ' ' && Grid[1][0] == Grid[1][1] && Grid[1][0] == Grid[1][2] )
        {
            win = true;
        }
    
        if(Grid[2][0] != ' ' && Grid[2][0] == Grid[2][1] && Grid[2][0] == Grid[2][2] )
        {
            win = true;
        }
    
        if(Grid[0][0] != ' ' && Grid[0][0] == Grid[1][0] && Grid[0][0] == Grid[2][0] )
        {
            win = true;
        }
    
        if(Grid[0][1] != ' ' && Grid[0][1] == Grid[1][1] && Grid[0][1] == Grid[2][1] )
        {
            win = true;
        }
    
        if(Grid[0][2] != ' ' && Grid[0][2] == Grid[1][2] && Grid[0][2] == Grid[2][2] )
        {
            win = true;
        }
    
        if(Grid[0][0] != ' ' && Grid[0][0] == Grid[1][1] && Grid[0][0] == Grid[2][2] )
        {
            win = true;
        }
    
        if(Grid[0][2] != ' ' && Grid[0][2] == Grid[1][1] && Grid[0][2] == Grid[2][0] )
        {
            win = true;
        }
    
    }
    
    bool TicTacToe::isWin()
    {
        return win;
    }
    
    void TicTacToe::checkSpaceLeft()
    {
        int emptySpaces = 0;
        for (int a = 0; a<3; a++)
        for (int b = 0; b<3; b++)
        {
            if (Grid[a][b] == ' ')
            {
                emptySpaces++;
            }
        }
    
        if (emptySpaces == 0)
            spaceLeft = false;
    
    }
    bool TicTacToe::isSpaceLeft()
    {
        return spaceLeft;
    }
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by JonathanS
    Whenever I wanted to use header files I would just make a .cpp and .h file. However the professor I'm taking wants two .cpp files and a header (one cpp file having the same name name as the header, the other main).

    What is the purpose of the .cpp file with the same name and how do I structure it?
    A sensible approach is to define those functions declared in the .h in the corresponding .cpp file. Then, in main.cpp, you include the header and use the functions in the main function. In this way, it would become possible to write a different program by creating a main2.cpp file that defines another main function that does things a little differently, yet includes the same header.

    By the way, don't do this:
    Code:
    using namespace std;
    #include "TicTacToe.h"
    The using directive should come after the header inclusion, otherwise it affects the included code. Also, don't place using directives at file scope in header files. You should fully qualify the names instead.
    JonathanS likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your .h file should just have the class definition:
    Code:
    // TicTacToe.h
    
    #ifndef TICTACTOE_H
    #define TICTACTOE_H
    
    class TicTacToe
    {
        // ...
    };
    
    #endif
    The .cpp file should have the function definitions and should include the .h file.
    Code:
    // TicTacToe.cpp
    
    #include <iostream>
    #include "TicTacToe.h"
    using namespace std;
    
    TicTacToe::TicTacToe()
    {
       //...
    }
    
    etc.
    You can compile it like this:

    gcc main.cpp TicTacToe.cpp
    JonathanS likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Well TicTacToe.cpp would be as follows:
    Code:
    #include <iostream>
    #include "TicTacToe.h"
    // Then everything from line 43 onwards in your current TicTacToe.h file
    Also, since the .h file no longer has any implementation, you can remove these two lines.
    #include <iostream>
    using namespace std;

    It is common practice to use include guards in all header files, say
    Code:
    #ifndef TICTACTOE_H_INCLUDED
    #define TICTACTOE_H_INCLUDED
    
    class TicTacToe
    {
    	private:
    		char Grid[3][3];
    		int inputrow;
    		int inputcolumn;
    		bool RowOK;
    		bool ColumnOK;
    		bool win;
    		bool empty;
            char winningPlayer;
            bool spaceLeft;
    
    	public:
    		TicTacToe();
    		void outputGrid();
    
    		void getRow();
    		void checkRow();
    		bool checkRowOK();
    
            void getColumn();
    		void checkColumn();
    		bool checkColumnOK();
    
            void checkTile();
    		bool isEmpty();
    
    		void fillTile(char);
    
    		void checkWin();
    
    		bool isWin();
    
    		void checkSpaceLeft();
    		bool isSpaceLeft();
    
    };
    
    #endif
    Finally, add TicTacToe.cpp to the list of project source files (alongside main.cpp)
    JonathanS likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Thanks, problem solved
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do headers and other c files work
    By noob in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 07:04 PM
  2. Headers files for Windows?
    By hacinn in forum C Programming
    Replies: 5
    Last Post: 08-09-2005, 10:54 AM
  3. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  4. differance between headers and CPP files
    By phantom in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2002, 06:15 PM
  5. Beginner Requiring a little help
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-13-2002, 01:04 PM

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