Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-11-2004, 04:46 PM   PM User | #1
Code Wizard
Regular Coder

 
Join Date: Nov 2003
Location: Code Heaven
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Code Wizard is an unknown quantity at this point
Validating Input...

In C++ if an integer value is expected,and the user enters a character,what value shall the variable actually take?

Code:
int n;
cout<<"n=";
cin>>n;

And also,what sort of validation is it best to use?
For example,this won't work:

Code:
int n;
do
cout<<"Enter a number:";
while(!(cin>>n));
Code Wizard is offline   Reply With Quote
Old 04-12-2004, 02:04 AM   PM User | #2
Mhtml
Senior Coder

 
Mhtml's Avatar
 
Join Date: Jun 2002
Location: Sydney, Australia
Posts: 3,531
Thanks: 0
Thanked 1 Time in 1 Post
Mhtml is an unknown quantity at this point
Well, I hardly ever use console stuff but I believe that you can just do it like how you have done except instead of !cin>>n using cin.fail (it's a bool)..

Code:
int n;
do
cout<<"Enter a number:";
cin>>n;
while(cin.fail);
As for the value the variable will take when a char is entered, I don't think it will take any value. It isn't initialized in your example so it probably has some random number in it.
__________________
Omnis mico antequam dominus Spookster!
Mhtml is offline   Reply With Quote
Old 04-12-2004, 10:01 PM   PM User | #3
Code Wizard
Regular Coder

 
Join Date: Nov 2003
Location: Code Heaven
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Code Wizard is an unknown quantity at this point
Hmm..well it's quite odd that !cin>>n doesn't work,it should be working coz cin returns a bool value depending on it's succes in reading the value...
Code Wizard is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:46 PM.


Advertisement
Log in to turn off these ads.