Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unexpected result when entering wrong data type

Reply
Thread Tools

Unexpected result when entering wrong data type

 
 
japh
Guest
Posts: n/a
 
      01-08-2005
I am on chapter 7 of my C++ text (sigh). Could you help me understand
this? If the user inputs an int, the script works as it should. If
they enter a char instead, it goes into an endless loop. I'm coming
from un-typed languages like PERL and PHP so this makes no sense to me:
(PS - is there a way to preserve indentation in this bulletin board ?)

#include <iostream>
using namespace std;

int main()
{
while(true)
{
int choice;
cout << endl << "Enter a number between 1 - 5: ";
cin >> choice;

if (choice < 1 || choice > 5)
{
cout << endl << "Invalid choice" << endl;
continue;
}
else
{
cout << "You chose " << choice << endl;
}
}// end while
} // end main

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-08-2005
"japh" <> wrote...
>I am on chapter 7 of my C++ text (sigh). Could you help me understand
> this? If the user inputs an int, the script works as it should. If
> they enter a char instead, it goes into an endless loop.


This is actually a FAQ. Visit http://www.parashift.com/c++-faq-lite/
Read it all when you have time just to get an idea what has already
been answered, so you don't ask about it again (unless it's unclear
or you think it's incorrectly answered there). For now, you need the
section 15.

> I'm coming
> from un-typed languages like PERL and PHP so this makes no sense to me:
> (PS - is there a way to preserve indentation in this bulletin board ?)


Not if you're using Google's delapidated posting service. Get a real ISP
and connect to their news server using a real news reader.

> [...]


V


 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      01-08-2005
"japh" <> wrote in message
news: ups.com...
> I am on chapter 7 of my C++ text (sigh). Could you help me understand
> this? If the user inputs an int, the script works as it should. If
> they enter a char instead, it goes into an endless loop. I'm coming
> from un-typed languages like PERL and PHP so this makes no sense to me:
> (PS - is there a way to preserve indentation in this bulletin board ?)


The FAQ (http://www.parashift.com/c++-faq-lite/) answers this. See section
15 ("Input/output via <iostream> and <cstdio>"), question 2 ("Why does my
program go into an infinite loop when someone enters an invalid input
character?"). The other answers in that section are a good read as well.

--
David Hilsee


 
Reply With Quote
 
japh
Guest
Posts: n/a
 
      01-08-2005
OK - thanks... will do

 
Reply With Quote
 
japh
Guest
Posts: n/a
 
      01-08-2005
thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql::Result .each_hash - unexpected result Andy Tolle Ruby 7 11-15-2010 10:39 AM
Entering an enumeration type as a property Nathan Sokalski ASP .Net 6 11-07-2009 11:56 PM
java.sql.SQLException: JZ0P1: Unexpected result type. Bumsys@gmail.com Java 0 05-30-2008 03:09 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Error when entering wrong data type in do-while loop Jeff C++ 3 02-05-2005 06:56 PM



Advertisments