When i declare a variable as '1' it shows end up being '49'

This is a discussion on When i declare a variable as '1' it shows end up being '49' within the C++ Programming forums, part of the General Programming Boards category; so here is my code Code: #include <iostream> using namespace std; int main() { char board1('1'); char board2('2'); char board3('3'); ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Exclamation When i declare a variable as '1' it shows end up being '49'

    so here is my code
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char board1('1');
        char board2('2');
        char board3('3');
        char board4('4');
        char board5('5');
        char board6('6');
        char board7('7');
        char board8('8');
        char board9('9');
        int space;
        bool loop(true);
    
       do{
           int player='1';
    
        cout<<board1<<"|"<<board2<<"|"<<board3<<endl;
        cout<<"- - -\n";
        cout<<board4<<"|"<<board5<<"|"<<board6<<endl;
        cout<<"- - -\n";
        cout<<board7<<"|"<<board8<<"|"<<board9<<endl;
        cout<<"Player"<<player<<"'s turn\n";
        cin>>space;
        if(player == '1' && space == '1' && board1 == '1'){
            board1 = 'x';
            if(board2 == 'x' && board3 == 'x'){
                cout<<"Player 1 Wins!\n";
                loop = false;
            }
            if(board4 == 'x' && board7 == 'x'){
                cout<<"Player 1 Wins!\n";
                loop = false;
            }
        }
        if(player == '1' && board2 == '2' && space == '2'){
            board2 = 'x';
            if(board5 == 'x' && board8 == 'x'){
                cout<<"Player 1 Wins!\n";
            }
        }
       }while(loop == true);
    
    
        system("PAUSE");
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    The character '1', is not the same as the numeric literal 1. The numeric value of ascii character '1' is 49.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    OK I got ride of the quotes for player but, now it shows up as one but when i press "1" it stays the same.
    Last edited by bijan311; 02-06-2010 at 06:48 PM.

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Read about typing in C++ and ASCII.

    You can also run this test. Try to understand what is going on.

    Code:
    #include <iostream>
    int main()
    {
        int i;
        char c;
        i='1';
        c='1';
        std::cout<<"i="<<i<<' '<<"c="<<c<<std::endl;
        i=1;
        c=1;
        std::cout<<"i="<<i<<' '<<"c="<<c<<std::endl;
        return 0;
    }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    thanks

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    OK well now that i finished all of the places on the boards when i press 9, it stays the same. Here's the code.
    Code:
    if(player == 1 && space == 9 && board9 == '9'){
            board9 = 'x';
            if(board6 == 'x' && board3 == 'x'){
            cout<<"Player 1 Wins!\n";
            loop = false;
            }
            if(board8 == 'x' && board7 == 'x'){
            cout<<"Player 1 Wins!\n";
            loop = false;
            }
            if(board5 == 'x' && board1 == 'x'){
            cout<<"Player 1 Wins!\n";
            loop = false;
            }
        }

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Can you give a more detailed explanation? Particularly regarding the "it stays the same" part.

    I also want to mention that you can make your code much much... much more general. After you get this sorted out, I recommend you try to generalize it. You could easily reduce the number of lines of code by a factor of 2 or 3... or 4.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is my variable changing value?
    By Tigers! in forum C Programming
    Replies: 3
    Last Post: 09-15-2009, 05:24 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. declare variable in case statement
    By Marksman in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 02:32 AM
  4. Overwriting all in array, why?
    By guesst in forum C Programming
    Replies: 7
    Last Post: 10-09-2008, 05:56 PM
  5. appending some letters to the end of a CString variable.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 02:00 PM

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