Like Tree4Likes

Code not working w/ Char

This is a discussion on Code not working w/ Char within the C++ Programming forums, part of the General Programming Boards category; Ok, I have the following game board: 1 | 2 | 3 4 | 5 | 0 7 | 8 ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Code not working w/ Char

    Ok, I have the following game board:

    1 | 2 | 3
    4 | 5 | 0
    7 | 8 | 6

    You're supposed to win the game when your board looks like this:

    1 | 2 | 3
    4 | 5 | 6
    7 | 8 | 0

    Which means you'd have to swap the 0 for the 6.
    So for this example I defined an array of integers:
    Code:
    int GameBoard[9];
    GameBoard[0] = 1;
    GameBoard[1] = 2;
    (...)
    GameBoard[5] = 0;
    (...)
    GameBoard[8] = 6;
    
    //Checks for end game conditions
    if (Play == GameBoard[8])
        {
            if (GameBoard[7] == 0)
            {
                swap(GameBoard[8], GameBoard[7]);
                goto UpdateBoard;
            }
            if (GameBoard[5] == 0)
            {
                swap(GameBoard[8], GameBoard[5]);
                goto UpdateBoard;
            }
                    else {
                 cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
                 goto InitialBoard;
    
    
        }
    UpdateBoard:
        cout<<"You win".;
    Ok so the code works fine for the example above (Note that it will only work if you want to move the "6").

    My issue is I can't make it work with a char type array, because the "0" is supposed to be an empty space and not a number.
    I tried with the following code:

    Code:
    char GameBoard[9];
    GameBoard[0] = '1';
    GameBoard[1] = '2';
    (...)
    GameBoard[5] = ' '; 
    (...)
    GameBoard[8] = '6';
    
    //Checks for end game conditions
    if (Play == GameBoard[8])
        {
            if (GameBoard[7] == ' ')
            {
                swap(GameBoard[8], GameBoard[7]);
                goto UpdateBoard;
            }
            if (GameBoard[5] == ' ') //Note that I changed it from 0 to ' ' (Single space).
            {
                swap(GameBoard[8], GameBoard[5]);
                goto UpdateBoard;
            }
                    else {
                 cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
                 goto InitialBoard;
    
    
        }
    
    UpdateBoard:
        cout<<"You win".;
    Ok so I used the same method for an array of characters but it doesn't work as integers do.
    If I try to move the "6" piece, I will just get the error message "You can not move that piece. Please, try again!", but if you look closely GameBoard[7] is, in this case, an empty space and it should swap successfully, should it not?

    Thanks in advance.
    Last edited by Khabz; 03-15-2013 at 10:38 AM.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Maybe I'm missing something, but where are you actually assigning an initial value to GameBoard[7]?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    It's there, I just skipped to define the whole array on this code. It's on the original one though

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    I think the confusion may lie in the difference between integer 6 and the character '6'. Run this little program as a hint. (Note: I'm a 'C' guy, which explains the code, but it should run for you).

    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("The value of  integer  6 is:  %d\n",6);
        printf("The value of character 6 is:  %d\n",'6');
    
        return 0;
    }

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    So does that mean you're omitting part of your code? If so, please post your whole code. I have an inkling that you're running your program in debug mode in Visual Studio (right?). If this is the case, here's something you should be aware of. If I recall correctly, in a debug build Visual Studio automatically initializes arrays that are local to a function in a way that all their elements will be zero. In your case, that would lead to different behavior if your array elements are of type integer than if they were of type char. The reason for this is that the integer value of the space character (' ') is not zero.


    EDIT: I was totally wrong about the red part.
    Last edited by antred; 03-15-2013 at 11:20 AM.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Code:
    #include <algorithm>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    	char GameBoard[9];
    
    
    	GameBoard[0] = '1';
    	GameBoard[1] = '2';
    	GameBoard[2] = '3';
    	GameBoard[3] = '4';
    	GameBoard[4] = '5';
    	GameBoard[5] = ' ';
    	GameBoard[6] = '7';
    	GameBoard[7] = '8';
    	GameBoard[8] = '6';
    
    
    
    
    InitialBoard:
    
    
    	cout<<"  " <<GameBoard[0] <<"  |  " <<GameBoard[1] <<"  |  " <<GameBoard[2] <<endl;
    	cout<<"-----+-----+-----" <<endl;
    	cout<<"  " <<GameBoard[3] <<"  |  " <<GameBoard[4] <<"  |  " <<GameBoard[5] <<endl;
    	cout<<"-----+-----+-----" <<endl;
    	cout<<"  " <<GameBoard[6] <<"  |  " <<GameBoard[7] <<"  |  " <<GameBoard[8] <<endl <<endl;
    
    
    NextMove:
    
    
    	//Check end game conditions
    	if (GameBoard[0] == '1' && GameBoard[1] == '2')
    	{
    		if (GameBoard[2] == '3' && GameBoard[3] == '4')
    		{
    			if (GameBoard[4] == '5' && GameBoard[5] == '6')
    			{
    				if (GameBoard[6] == '7' && GameBoard[7] == '8')
    				{
    					if (GameBoard[8] == ' ')
    					{
    						goto EndGame;
    					}
    				}
    			}
    		}
    	}
    
    
    
    
    	int Play;
    	cout<<"Choose a number to move: ";
    	cin>>Play;
    	cin.ignore();
    	
    	
    	//Checks for (in)valid moves
    	if (Play == GameBoard[0])
    	{
    		if (GameBoard[1] == ' ')
    		{
    			swap(GameBoard[0], GameBoard[1]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[3] == ' ')
    		{
    			swap(GameBoard[0], GameBoard[3]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[1])
    	{
    		if (GameBoard[0] == ' ')
    		{ 
    			swap(GameBoard[1], GameBoard[0]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[2] == ' ')
    		{
    			swap(GameBoard[1], GameBoard[2]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[4] == ' ')
    		{
    			swap(GameBoard[1], GameBoard[4]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[2])
    	{
    		if (GameBoard[1] == ' ')
    		{
    			swap(GameBoard[2], GameBoard[1]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[5] == ' ')
    		{
    			swap(GameBoard[2], GameBoard[5]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[3])
    	{
    		if (GameBoard[0] == ' ')
    		{
    			swap(GameBoard[3], GameBoard[0]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[4] == ' ')
    		{
    			swap(GameBoard[3], GameBoard[4]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[6] == ' ')
    		{
    			swap(GameBoard[3], GameBoard[6]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[4])
    	{
    		if (GameBoard[1] == ' ')
    		{
    			swap(GameBoard[4], GameBoard[1]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[3] == ' ')
    		{
    			swap(GameBoard[4], GameBoard[3]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[5] == ' ')
    		{
    			swap(GameBoard[4], GameBoard[5]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[7] == ' ')
    		{
    			swap(GameBoard[4], GameBoard[7]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[5])
    	{
    		if (GameBoard[2] == ' ')
    		{
    			swap(GameBoard[5], GameBoard[2]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[4] == ' ')
    		{
    			swap(GameBoard[5], GameBoard[4]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[8] == ' ')
    		{
    			swap(GameBoard[5], GameBoard[8]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[6])
    	{
    		if (GameBoard[3] == ' ')
    		{
    			swap(GameBoard[6], GameBoard[3]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[7] == ' ')
    		{
    			swap(GameBoard[6], GameBoard[7]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[7])
    	{
    		if (GameBoard[6] == ' ')
    		{
    			swap(GameBoard[7], GameBoard[6]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[4] == ' ')
    		{
    			swap(GameBoard[7], GameBoard[4]); 
    			goto UpdateBoard;
    		}
    		if (GameBoard[8] == ' ')
    		{
    			swap(GameBoard[7], GameBoard[8]);
    			goto UpdateBoard;
    		}
    	}
    	if (Play == GameBoard[8])
    	{
    		if (GameBoard[7] == ' ')
    		{
    			swap(GameBoard[8], GameBoard[7]);
    			goto UpdateBoard;
    		}
    		if (GameBoard[5] == ' ')
    		{
    			swap(GameBoard[8], GameBoard[5]);
    			goto UpdateBoard;
    		}
    	}
    	else {
    		cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
    		goto InitialBoard;
    	}
    
    
    UpdateBoard:
    	cout<<"  " <<GameBoard[0] <<"  |  " <<GameBoard[1] <<"  |  " <<GameBoard[2] <<endl;
    	cout<<"-----+-----+-----" <<endl;
    	cout<<"  " <<GameBoard[3] <<"  |  " <<GameBoard[4] <<"  |  " <<GameBoard[5] <<endl;
    	cout<<"-----+-----+-----" <<endl;
    	cout<<"  " <<GameBoard[6] <<"  |  " <<GameBoard[7] <<"  |  " <<GameBoard[8] <<endl;
    
    
    
    
    	goto NextMove;
    
    
    EndGame:
    
    
    	cout<<"You win! :)";
    
    
    	cin.get();
    
    
    	}

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'm aware '6' is a char and 6 is an integer.
    My point is, my code works for an array of integers (6) but it does not work for an array of chars ('6').

    It fails to check the (in)valid moves.

    Code:
    if (Play == GameBoard[8])    
    {        
          if (GameBoard[7] == ' ')        
         {            
                swap(GameBoard[8], GameBoard[7]);            
                goto UpdateBoard;        
          }        
          if (GameBoard[5] == ' ')        
          {            
                swap(GameBoard[8], GameBoard[5]);            
                goto UpdateBoard;        
           }
     }
    If I define the array as int:
    The code above works if you swap " ' ' " with "0", which means it recognizes GameBoard[7] to be 0,
    If I define the array as char:
    The code above does NOT recognize GameBoard[7] as a space ( ' ' )


    And yes, I'm using Visual studio
    Last edited by Khabz; 03-15-2013 at 11:00 AM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    The code above does NOT recognize GameBoard[7] as a space ( ' ' )
    This is not the problem. If you enter 6, you're not even getting to the GameBoard[7] comparison.

    Code:
        if (Play == GameBoard[8])  // Play == 6, GameBoard[8] == '6', and 6 != '6'
        // so the entire following block is skipped
        {
            if (GameBoard[7] == ' ')
            {
                swap(GameBoard[8], GameBoard[7]);
                goto UpdateBoard;
            }
            if (GameBoard[5] == ' ')
            {
                swap(GameBoard[8], GameBoard[5]);
                goto UpdateBoard;
            }
        }
        // and this is executed instead
        else {
            cout<<"You can not move that piece. Please, try again!" <<endl <<endl;
            goto InitialBoard;
        }
    If I try to move the "6" piece, I will just get the error message "You can not move that piece. Please, try again!"
    Khabz likes this.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Ok I got it, because the user is inserting an integer and not a character, am I right?

    I changed from:

    Code:
    int Play;
    to:

    Code:
    char Play;
    EDIT: It worked, forgot to change a small detail. Silly me!

    Thank you so much for your help ! Failed to see a simple thing as that !
    Last edited by Khabz; 03-15-2013 at 11:17 AM.

  10. #10
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Hmm, I've compiled and tested your program with that correction (i.e. turning int Play; into char Play;) and it appears to work.

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    I also want to point out that what I said in post #5 appears to be nonsense. Visual Studio does no such thing.

  12. #12
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yes it is working now, I highly appreciate!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    The next thing to work on would be those gotos. Gotos create difficult-to-follow program flow. You should learn to use proper control structures (loops).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Well the main problem I can see is that you have been infected by the dreaded goto bug.

    Basically at some point during learning, before the newbie has thoroughly learned proper use of program control statements, they become inadvertantly exposed to this hazardous thing known as "goto". This goto thing then invades their programs and multiplies in order to try and kill the program, and destroy the programmer's sensibility. Before long, they lose the ability to recognise simple logical constructs such as loops.
    Exposure to other programming languages which do not contain this goto can help to immunise the programmer against goto, but can have other unwanted side-effects.
    Prevention is generally better than cure, and the road to recovery can be a long one of retraining and rehabilitation. Prognosis improves the sooner treatment is started.
    Elysia and rogster001 like this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'm quite aware 'gotos' are the easy but hazardous solution. Might have used them for some begginer programs where there's no way you can get lost, but I am definitely changing the way I think.
    On my way to edit the algorithm.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with char
    By Mentallic in forum C Programming
    Replies: 12
    Last Post: 09-06-2011, 09:35 AM
  2. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  3. Copying one char* to another not working?
    By Saggio in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 07:03 AM
  4. map<char*, word> not working
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 03-13-2006, 02:02 AM
  5. working with char
    By kennny2004 in forum C++ Programming
    Replies: 20
    Last Post: 11-07-2005, 06:33 AM

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