Thread: Char variable can't store numbers?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Char variable can't store numbers?

    I tried to make a char variable, which can store any symbol, right? So I set it to "1", which got me a bunch of errors, then I set it to 1, and it turned into this odd smiley face thing. Can someone help me?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some code, and a bit more of an explanation of what you want to do.

    Also, you might want to read a tutorial or the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    I did read those. It's just like char a=1. I'm trying to get it so that when I put in cout a; I get 1, which a is, not some smiley face

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    int i = 1;
    std::cout <<i <<std::endl;
    Ideally int's are for numbers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Yeah, I know, but it's a tic-tac-toe game and it would just be easier to output all the variables, and if theres an X or O in the script, then I don't have to go through the long and annoying process of changing it all. I don't know if that made sense. Imagine this is my tic-tac-toe board:

    1|2|3
    -------
    4|5|6
    -------
    7|8|9

    If I have a char for every "slot", then when it's empty it displays its number, so that you can input to choose that one. If it has something in it, the variable changes and no longer displays its number. As in:

    X|O|X
    -------
    4|O|6
    -------
    X|X|O

    This can use the same code as the last example, because it's just outputting all the variables again. I don't know if that made any sense.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Take your pick:
    Code:
      char c = '1';
      cout <<c << endl;
    
    // or
    
      c = 1;
      cout <<(int)c <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    When setting the char, does it matter if you use ' or "? If it does, I feel really stupid

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Kespoosh
    When setting the char, does it matter if you use ' or "? If it does, I feel really stupid
    check my post above.

    Yes it does matter. double quotes are for strings (char arrays), single quotes are for single chars.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Alright, thanks. Now I have another problem.
    Code:
    char a='1';
    cout a;
    This displays 49. Why?

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    think 49 is the ascii value for '1'.

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Are you sure you didn't type this:
    Code:
    int a='1';
    cout<<a;
    49 is the ASCII value of '1'. If you declare it as a char though, and display it, it should say 1. If not, cut and paste your whole code here for us to look at.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    AHA! Yes! Thank you. That fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM