Int, Char mix up

This is a discussion on Int, Char mix up within the C++ Programming forums, part of the General Programming Boards category; Hi I apologise that this has most likely been asked many many times (so obviously if it's anoying you, dont ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Talking Int, Char mix up

    Hi I apologise that this has most likely been asked many many times (so obviously if it's anoying you, dont worry about repying )

    Anyway If I got a while loop where the condition needs an int to end the loop, how can I catch a error if someone enters a character instead, because then I just get a indefinite loop.

    For example if I create a variable of type Int Answer
    it's looking for a integer but as soon as I put a character I get all sorts of fun things happen, would an If statement be the way past this?

    I appreciate your time in answering in advance.

    Chris

    Fourm Rebel with a cause!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    cin goes into an error state if that happens, which means it will ignore all further read requests. You can test this state with the fail() or good() methods, or just direct bool conversion:
    Code:
    if(cin) {
      // cin is fine
    }
    if(!cin) {
      // cin is not fine, either in error state or at end of file
    }
    You can call clear() to reset cin, but you also need to call ignore() to get rid of the invalid characters in the input buffer.

    P.S: What's a "fourm rebel"?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    chars are really just small ints.

    use isdigit() to determine whether your char is a digit or not. isdigit is declared in ctype.h.

    in your case I'd use a switch() statement. you can also use an else-if to achieve the same test, but if you are wanting to test against several different characters than a switch() might be a better alternative.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It is easy to use a while loop to check for the int and prompt the user to try again if it is not a vali integer.
    Code:
    int value = 0;
    // Prompt for input
    while (!(std::cin >> value))
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      // Tell the user they typed in bad input here and prompt again.
    }
    This is simple and works in most every case. One case where it might not work is if you want it to error out when the user types extra characters within or after the number (e.g. 1234abc or 123r5). To test for that, simply change the while loop to:
    Code:
    int value = 0;
    // Prompt for input
    while (!(std::cin >> value) || std::cin.get() != '\n')
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      // Tell the user they typed in bad input here and prompt again.
    }
    Note that you have to #include <limits> to use numeric_limits. You can also just use a large number there.
    Last edited by Daved; 01-20-2006 at 10:18 PM. Reason: Thanks for the correction CornedBee

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    Thanks

    Thanks Guys, very kind of you!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    while (!(std::cin >> value) && std::cin.get() != '\n')
    I believe you must use || instead of && here.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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