CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    15

    Resolved Exception - inputted a character for an integer

    Hi,

    i accidentally given input as character for a integer variable in the console.....the program went crazy and just flooded the console.....

    so, is there any way to handle this exception......
    how to achieve that

    thank you

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Exception - inputted a character for an integer

    I guess you use scanf().
    Use one or several fgetc() instead.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Exception - inputted a character for an integer

    You simply have to test whether the input was successful. If not, you'll need to clear the error flags on the stream before trying again.

    It would help to know if you're using <cstdio> IO (scanf, etc) or <iostream> IO (ifstream, cin, etc).

  4. #4
    Join Date
    Sep 2010
    Posts
    15

    Re: Exception - inputted a character for an integer

    Quote Originally Posted by Lindley View Post
    You simply have to test whether the input was successful. If not, you'll need to clear the error flags on the stream before trying again.

    It would help to know if you're using <cstdio> IO (scanf, etc) or <iostream> IO (ifstream, cin, etc).
    okay....thank you...

    i used cin.ignore,
    cin.clear, cin.fail

  5. #5
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Exception - inputted a character for an integer

    Read this one.
    http://www.parashift.com/c++-faq-lit....html#faq-15.3

    In fact just for fun read the entire section 15. This is critical information for all beginners who are writing console applications.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured