Need Some C++ Help
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Need Some C++ Help

  1. #1

    Need Some C++ Help

    Well, I suppose I will give this forum it's first thread.
    Im working on a program for a school assignment.
    It's not too advanced. It compiles just fine. Im just getting a logic error.
    The program is a game, played between you and the computer. Players
    alternate turns putting down pieces randomly till they get five in a row
    in either direction.

    Most of this hasn't been implimented yet. Here is what has...

    After Running TestGame you are prompted to enter if you want to be b or w.
    Enter either b or w then the game displays a board of empty spaces (0's)
    Then it asks you to enter a char/int combo. So enter something like a2
    From there, the data is entered into the board, and your letter (b or w) is in place of the zero.
    The problem is, when I print the board for the second time, all that shows up is zeros.
    I know the data is actually being put into the board becasuse I have called the data at the location in the array specifically and it has shown that it is there. I think my problem is in BoardClass.cpp in the function PrintBoard. And Im sure the problem is with the for statements.

    I just can't figure out why the board keeps printing a "blank" board. Any help would be nice.

    The file is attached.

  2. #2
    Senior Member
    Join Date
    May 2002
    Posts
    344
    hey volcanic, sorry i didnt get to your post earlier my internet went down. Anyways, i didnt download your code cause i have to finish all my chemistry homework and i dont have enough time. Anyways, so let me try to get this straight...you enter either b or w then enter a char/int combo and you then either 'b' or 'w' get positioned on the place that you entered as your position (your char/int combo). Am i right? Well it sounds like your problem is that when the players second turn rolls around, the board calls the function PrintBoard and dumps out your previous 'b' or 'w' position and remakes a new board with your new 'b' or 'w' position. It sounds like the problem is with your variables duration, they are created when the function is called and are cleared when the function is recalled. I dont know if what i am saying even has to do with your problem because i havent had the chance to download all your code, but thats an error that people frequently make, instead of keeping the old position and adding on to it, you are just clearing the old position (the char/int thing) and replacing it with your new position (the second char/int combo you enter). Anyways i hope i helped...let me know i will re-visit this problem tomarrow and download the code sorry kinda in a time crunch
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  3. #3
    There is no second turn yet, the program is only designed to imput 1 piece onto the board, and then stop.

  4. #4
    Senior Member
    Join Date
    May 2002
    Posts
    344
    lol so i was completly off, alright i will download the stuff tomarrow and i will edit this post when i get it all figured out...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  5. #5
    I will probably have it figured out by then, but in the interest of those who care, I will post the answer to my problem.

    Thanks for trying to help.


  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Alright i cant edit my post so here is a new one...i think i found your error. if you go to TestGame.cpp and look at this bit of code:
    Code:
    		if (color == 'b')
    		{
            	cout << "Enter your move (rowcolumn) a-h|1-8:: ";
            	cin >> currentMove;
    			//changes letters to numbers
    			if (currentMove[0] == 'a')
    			{
    				move.row = '1';
    			}
    			if (currentMove[0] == 'b')
    			{
    				move.row = '2';
    			}
    			if (currentMove[0] == 'c')
    			{
    				move.row = '3';
    			}
    //etc...
    Notice this comment in your code:
    //changes letters to numbers
    That isnt correct, it doesnt change letters to numbers, although thats what it looks like it is doing. it is just chaing letters to other letters. both 'a' and '1' are two different characters, '1' isnt an integer. Alright, so that compiled fine cause a character can be transformed into an integer (buts thats not what you wanted). Anyways, you then went on and wrote this line of code:
    gameBoard->EnterMove(gameBoard, move.row, move.column, 'b');
    The problem with that line is that move.row is not taking in an integer, as it should do, but instead it is taking in a character. If you look bellow you will see the problem this will create...

    Now look at your function void EnterMove(BoardRecord *gameBoard, int pRow, char pCol, char pPieceColor) in BoardClass.cpp

    ok damn this is a bitch to explain online...well look at your function's signature...like i said above, you typed gameBoard->EnterMove(gameBoard, move.row, move.column, 'b'). move.row is suppose to match up with int pRow, therefore the character '1' gets transformed into its ANSII value which is 49. look at your code bellow sorry this post is taking so much room...please bear with me:
    Code:
    		Row = pRow;
    		Col = pCol;
    		PieceColor = pPieceColor;
    		if (PieceColor == 'b')
    		{
    		cout << "Piece Color is: " << PieceColor << "\n\n";
    		gameBoard->Board[Row].Rows[Col].Piece = PieceColor;
                    }
    So lets just look at this code...your variable Row now contains the integer 49 NOT 1 like you wanted it to. Then on your last line of code in this little quote thing is gameBoard->Board[Row].Rows[Col].Piece = PieceColor; Well the problem here is that Row contains 49 NOT 1.

    Alright so basically you are mixing integers with characters by trying to preform your char/int combo thing. When you assign a character value to a variable that is suppose to hold an integer, the variable will take the ANSII value of that character and hold it. Anyways i hope this solves your problem...let me know if you need more help

    Well just a 15 year old's 2 cents...
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  7. #7
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    White_Eskimo,
    15 heh. Are you self taught or do you take CS in school? Also, are you trying to make fun of everyone else, because now I feel stupid.

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    No when i was 13 i wanted to be a 1337 haxwh0r3 and i thought i was a really hardcore hacker until i joined Antionline.com...then i realized i was what you call a script kidee or whatever so i decided to go to my local community college to take classes in C++. Thats how i learned and also at my school i am in AP Comp Sci but we are learning Java and i hate it I still am a script kidee but at least i know a little bit about programming
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  9. #9
    Dude, white_eskimo. You are awesome. I figured out my problem before I read your reply. It took about an hour, but it is exactly the same thing you found. I really congratulate you on your intelligence and on your patience to look through my crappy code.

    Im a bit more used to the java rule where an array size is set in stone. I almost wish the c++ compiler wouldnt have let me place something into spot 49 on the array. It would have helped so much. I still cannot belive I made such an elementary error.

    I appreciate your effort.

  10. #10
    Senior Member
    Join Date
    May 2002
    Posts
    344
    dude, white_eskimo. You are awesome.
    Glad i could help you out sorry it was a little late i had my stupid chemistry homework Anyways, let me know if you ever run into another error while coding
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides