Results 1 to 4 of 4
  1. #1
    Senior Member
    Join Date
    May 2003

    Post C++ Input Stream Failure

    Is there any way in C++ to detect an input stream failure without any action taken by the user and to clear it automatically? I'm noticing in some of the programs i write, many users acidentally enter a number into a char variable or letter in to an int variable, which causes a input stream failure.

    \"Look, Doc, I spent last Tuesday watching fibers on my carpet. And the whole time I was watching my carpet, I was worrying that I, I might vomit. And the whole time, I was thinking, \"I\'m a grown man. I should know what goes on my head.\" And the more I thought about it... the more I realized that I should just blow my brains out and end it all. But then I thought, well, if I thought more about blowing my brains out... I start worrying about what that was going to do to my goddamn carpet. Okay, so, ah-he, that was a GOOD day, Doc. And, and I just want you to give me some pills and let me get on with my life. \" -Roy Waller

  2. #2
    Senior Member
    Join Date
    May 2002
    umm, well what you are talking about are logic errors, and yes there are ways to fix them. A user can enter an int when the program demands for a char, because an integer is a type of character. When a user enters a character instead of an integer, the program will just take the caracter and turn it into ANSII or whatever (So if i enter '@' instead of an int, the int really is holding the value 100, the ANSII character value of @). Umm you could just add a function to your program that checks to make sure the integer inserted isnt a character by creating a string and everytime the user inserts something, call the function, have it run through the string of all possible characters, and if what the user entered matches up with one of the characters, just make it print error or something...
    Support your right to arm bears.

    ^^This was the first video game which i played on an old win3.1 box

  3. #3
    Senior Member
    Join Date
    Oct 2001
    ...not exactly sure if White_Eskimo's saying entering a character into an integer and having it converted into an integer on its ASCII value is true... My understanding is that since you can't store a character into an integer, the program usually freaks out unless you programmed a way around that. Maybe if you type cast it (I have problems with that) it will work, but otherwise won't since the data size of a single character and a single integer are (usually) different...

    So, write a piece of code that you can use over and over again for all of your areas for input. Since you can convert strings to numbers with some basic functions, you can get all of your input as strings, and convert them as needed. If I remember correctly, these functions will return an error if there is nothing to convert (no numbers in the string/input) and you can have your program ask for the user to type it in again by having this code in a while loop, or other loops. Of course there is still potential for a buffer overflow if you don't harden this piece of code by having it limit the length of input as needed and then clearing the input buffer, but after that is done your program will be much stronger against these attacks/issues. Just be sure to use that piece of code often at all input points, and make sure it is tweaked just right for the current piece of input.

  4. #4
    Junior Member
    Join Date
    Oct 2002
    Look up the fail, bad, clear, and ignore member functions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

By using this site, you agree to the Privacy Policy