why can't you assign a char to a char?

This is a discussion on why can't you assign a char to a char? within the C++ Programming forums, part of the General Programming Boards category; i've been using c++ for a while now, but I've never needed to use a single char variable. When I ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    why can't you assign a char to a char?

    i've been using c++ for a while now, but I've never needed to use a single char variable. When I try to compare if a char equals a const character i get flagged for an error.

    PHP Code:
    char choose '';
    cin.get(choose);

    if (
    choose == 'x') || (choose == 'X')
    {
     .
     .
     .

    whats wrong with this code?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
     if ( (choose == 'x') || (choose == 'X') )

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Quote Originally Posted by yahn
    i've been using c++ for a while now, but I've never needed to use a single char variable. When I try to compare if a char equals a const character i get flagged for an error.

    PHP Code:
    char choose '';
    cin.get(choose);

    if (
    choose == 'x') || (choose == 'X')
    {
     .
     .
     .

    whats wrong with this code?
    Your parenthesis are wrong with that code.

    Code:
    if (choose == 'x' || choose == 'X') {}
    Sent from my iPadŽ

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This should not compile
    Code:
     char choose = '';
    there is no such thing as an empty character.
    Kurt

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    thanks for the help. i just realized how much i suck at c++ still. and while i'm asking questions, would anyone care to tell me why cin.get(choose) isn't waiting for input? here is the full code

    PHP Code:
            cin.get(choose);
            
            if ((
    choose == 'x') || (choose == 'X'))
            {...
            } 
    that is going to the if statement before any input can be entered. when i use cin >> choose it waits, but i don't want to use cin >> choose because i only want one character.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by yahn
    twhen i use cin >> choose it waits, but i don't want to use cin >> choose because i only want one character.
    you can use
    Code:
    cin >> choose;
    to input one character as well. But that should be the same as cin.get(choose);
    If that doesn't work for you then it probably has to do with what you do before that statement.
    Kurt

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    hmm. I thought cin.clear() would take care of it. I guess you have to use ignore as well. Is there a way to just empty the istream? I always thought clear did that.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    would anyone care to tell me why cin.get(choose) isn't waiting for input?
    Read statements don't have to wait for the user to enter input, unless the input buffer is empty. Obviously, the input buffer still has something in it left over from previous user input that wasn't read.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by yahn
    hmm. I thought cin.clear() would take care of it. I guess you have to use ignore as well. Is there a way to just empty the istream? I always thought clear did that.
    No clear() only resets any fail-bits in the stream. to empty the buffer you need to call ignore().
    Kurt

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    hmm. i guess that makes sense then. well thank you agian for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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