Thread: char vs int - program crash!!

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    18

    char vs int - program crash!!

    Hi!

    How do i prevent a program to crash if it expects and int and receives in the input a char?

    and vice versa?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    cin.ignore(putshizhere) I think

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do i prevent a program to crash if it expects and int and receives in the input a char?
    The same way you prevent a crash for anything else. You prepare for it:
    Code:
    int x;
    
    while ( !( cin>> x ) ) {
      cerr<<"Invalid input\n";
      cin.clear();
      cin.ignore ( BIG_NUMBER, '\n' );
    }
    The !() wrapper around the input request basically says "did this request fail?". The cin.clear() part clears the error state of reading an unexpected character. If you don't do that, you won't be able to do much else with the stream. The cin.ignore part reads and discards whatever is left in the stream, up to either BIG_NUMBER characters, or a newline. It's much better to use the maximum value of the stream size than to guess at the value of BIG_NUMBER, which would look more like this (you have to include <limits>, for numeric_limits<> and <ios>, for streamsize):
    Code:
    int x;
    
    while ( !( cin>> x ) ) {
      cerr<<"Invalid input\n";
      cin.clear();
      cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    }
    The other way around is harder because a numeric digit is still a valid character. In that case, you need to validate your input one way or another. The FAQ at www.cprogramming.com has a number of ways to go about that, and it's very application specific.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A common question that follows this one is how to handle the case when the user types other things after the int, like 148abc. It only takes a small change to Prelude's code:
    Code:
    while ( !( cin>> x ) || cin.get() != '\n' ) {
      cerr<<"Invalid input\n";
      cin.clear();
      cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Great!!

    Thanks


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM