Thread: Error-handling code

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    Error-handling code

    Hey everyone, I have some more questions for the community ...I promise when I learn enough I will pass my knowledge to every human being in my path of programming destruction! haha everyone here's been (and hopefully will continue to be ) disturbingly helpful...that's right! I went there! So anyways, now that the ranting and raving's outta the way...here's the code and following is the QUESTION! bum bum bum:
    Code:
    if (!cin)    //bad input
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Bad input; input process terminated.\n";
                break;
            }
    The question here is what exactly does the
    while (cin.get() != '\n')
    continue;
    section do? Does it help in the removal of the bad input? As much information on this subject as well as what the continue in there does...and if you feel like discussing the entire chunk of code...by all means, that would be even greater, I didn't want to be too much of a hassle...hey if anyone's getting any help from my questions other than me, speak up...I feel like the annoying little brother who keeps jumping up and down begging for programing attention hahaha...alright, thanks everyone -Chap

  2. #2
    im pretty new to thsi stuff to, and would be useful, but
    as far as i know, this chunk code seems to means to me


    if the input is bad, it clears all of the input so far
    then it runs a while statment which reads characters until the
    character is '\n', which is a new line, or when you press enter,
    however you look at it.

    then if all is good it continues to the next segment of code, if
    something is wrong, then it displays the cout segment
    which tells you on screen there is an error, and then break
    ends the program or that section of code im not to sure, but
    that basic of what that does i believe, feel free to correct me
    cause ive onyl been programming on a regular basis for a little
    while.
    Last edited by JarJarBinks; 09-20-2004 at 03:29 PM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    right wher you have it is fine. but keyword continue isn't necessary and since the loop will have ended by the time you have keyword break, that isn't necessary either. Also what happens if the user hits the enter key repeatedly, but no other key? Probably better to be sure you clear the input buffer using something like this:
    Code:
    if(!cin)
    {
       cin.clear();
       cin. ignore(std::numeric_limits<std::streamsize>::max())
       cout << "bad input" << endl;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    right - 'flushing' cin's input buffer to prepare it for the next input event.

    >> as well as what the continue in there does

    'continue' causes execution to jump back to the beginning of the loop, bypassing any code that follows, ie:

    Code:
     while(1) {
      cout << "blah" << endl;
      continue;
      cout << "this will never print" << endl;
      }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I've always used
    cin.ignore(cin.rdbuf()->in_avail());
    to clear cin. This ensures that the clearing operation will not block and that everything that's there gets removed.
    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. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM