Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Elysia

Exceptions in bad stream

This is a discussion on Exceptions in bad stream within the C++ Programming forums, part of the General Programming Boards category; Hello, if a user was to enter a letter instead of an integer, the loop goes wild. I know how ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Exceptions in bad stream

    Hello,
    if a user was to enter a letter instead of an integer, the loop goes wild.
    I know how to get rid of that error
    cin.clear() and cin.ignore()
    but I haven't been able to understand why it happens. A character is represented by an integer from 0 to 255. And each number is 32 bit enough for the integer variable to handle..so why does the stream fail then?
    Is it possible to use exception to test for this errors, I am trying to understand exceptions, and I wasn't able to write code for that..
    Thanks
    You ended that sentence with a preposition...Bastard!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    When you input an integer, the allowed characters are 0 to 9 inclusive.

    When you type in 123, you expect 123
    Not some large number generated from the ASCII values 49(for '1'), 50(for '2'), 51(for '3')

    So if you type in "Hello" when you're expecting an integer, then yes the stream goes into the fail state.
    Eman likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh wow..yeah I think see you what you mean..
    it makes sense for "Hello" or a string but not if i type a single character..'a' is enough to send the loop going crazy..so it still makes little sense to me
    how is it that '0' to '9' are the allowed characters..it isn't possible for a user to enter input a numeric character...?
    You ended that sentence with a preposition...Bastard!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Strictly speaking, 'a' is an integer in the compiler's eyes. However, 'a' is not semantically an integer.
    It makes no sense to store an 'a' in an integer, because 'a' is a letter, not a number.
    Eman likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    ah right..makes sense then. Thanks
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exceptions
    By Yarin in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2008, 05:47 AM
  2. exceptions
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2006, 04:13 PM
  3. exceptions
    By Corrington_j in forum Linux Programming
    Replies: 6
    Last Post: 09-14-2005, 09:32 AM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. need help with exceptions
    By grimjb in forum C++ Programming
    Replies: 1
    Last Post: 10-05-2001, 08:05 PM

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