Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 5th, 2006, 5:10 AM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Defining input: int or char?

Hello! (First post). I am new to C++ and I have written a small program in C++ and now I want it to handle errors.

So, I want a way to display an error message if the user has given a character from the keyboard where he should give an integer. I know a little about program exceptions, but I fail to think of a way to make the program know if the user has given a character as an input instead of an int.

That way I will be able to make the program terminate before it displays the wrong result.

So, how can I do this?

Thank you in advance.
Soulstorm is offline   Reply With Quote
Old Jan 5th, 2006, 6:25 AM   #2
sunnypalsingh
Programmer
 
sunnypalsingh's Avatar
 
Join Date: Dec 2005
Posts: 33
Rep Power: 0 sunnypalsingh is on a distinguished road
See This
#include <iostream>

using namespace std;
int main()
{
     int x=0;
		 cout<<" Please Enter An Integer:";
	 if(cin>>x)
		 cout<<" Success...Number Entered Is:"<<x<<endl;
	 else
	     cout<<" You Have Not Entered An Integer"<<endl;
return 0;
}
__________________
The only real valuable thing is intuition.
-Albert Einstein
sunnypalsingh is offline   Reply With Quote
Old Jan 5th, 2006, 6:37 AM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Thanks... That worked nicely!

Although there is something I don't understand.

if(cin>>x)
That means that every time that a cin is performes, there is also a boolean operation taking place or...?

I mean that I have never seen this kind of code in the book I use ("C++ from the ground up"-Herbert Schildt). Are there other functions that can be treated this way?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jan 5th, 2006, 7:39 AM   #4
sunnypalsingh
Programmer
 
sunnypalsingh's Avatar
 
Join Date: Dec 2005
Posts: 33
Rep Power: 0 sunnypalsingh is on a distinguished road
Herbert Schildt!!!...I really don't wanna say anything about this.

Decision is made based on the "signal" provided by the stream "cin":
if(cin >> x)

If the input stream "cin" cannot acquire an integer (from the keyboard in this case), it provides the boolean value "false" and the enclosing "if-test" fails.
__________________
The only real valuable thing is intuition.
-Albert Einstein
sunnypalsingh is offline   Reply With Quote
Old Jan 5th, 2006, 12:58 PM   #5
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Originally Posted by sunnypalsingh
Herbert Schildt!!!...I really don't wanna say anything about this.

Decision is made based on the "signal" provided by the stream "cin":
if(cin >> x)

If the input stream "cin" cannot acquire an integer (from the keyboard in this case), it provides the boolean value "false" and the enclosing "if-test" fails.
Thabks. Herbert has written a very good book. This little detail is the only thing that it misses, apparently.

Thanks a lot for the information.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jan 5th, 2006, 1:29 PM   #6
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by Soulstorm
Thabks. Herbert has written a very good book. This little detail is the only thing that it misses, apparently.

Thanks a lot for the information.
Herbert Schildt it one of the least recommended authors for programming topics.

http://www.accu.org/bookreviews/publ.../c/c002173.htm Just look at all the books he's written, and how all of them are [Not Recommended].

Now this isn't my personal opinion of him. I haven't read any of his books, but if I had to choose between a book written by an author who I've heard isn't the best author on the subject and another author...I'd choose the other author.

You can read the reviews to find out why people dislike him.
Jason Isom is offline   Reply With Quote
Old Jan 5th, 2006, 3:17 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
While I don't claim to be of the calibre of Francis Glasborow, I fully agree with his views on Schildt's books. The best that can be said for the books I have read are that they are written in a style that is easy to read and seems authoritative; they will satisfy a publisher because of that, and someone looking for a book on C or C++ in a bookstore will initially think that they have found a real gem and could well buy it. The problem with them is that they include basic technical errors, code examples that are neither valid C nor valid C++, encourage very bad programming habits, and assume that all programmers target windows (or, in some of his older books, a particular compiler under MS-DOS).

That aside, I'll come back to the question of "if (cin >> x)" or (more simply) "if (cin)"....

One of the characteristics of the way I/O stream classes work in the C++ library work is that, if an error occurs when reading from a stream (or, in the case of ostreams, writing to one) that the object stores information about it's integrity and error status. Errors may occur when opening a stream (eg if an input file cannot be found), while doing any I/O with it This information can be sliced and diced using various various member functions (eg eof() to test if end of file has been reached, good() to see if the stream is OK, etc).

The simple minded tests "if (the_stream)" and "if(!the_stream)" work because the stream classes (actually one of it's base classes, but that's an implementation detail) support operator functions. "if(the_stream)" actually invokes a conversion operator for converting the stream to a void pointer and, if an error has occurred with the stream, that operator returns a NULL pointer. "if(!the_stream)" invokes a conversion operator for converting the stream to a bool, and that operator returns true if an error has occurred.
grumpy is offline   Reply With Quote
Old Jan 5th, 2006, 3:28 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Not sure if anyone else mentioned this, but you may want to look into

try { ... } catch { ... }

for exception handling also.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 5th, 2006, 4:37 PM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by grumpy
While I don't claim to be of the calibre of Francis Glasborow, I fully agree with his views on Schildt's books.
I didn't know Francis Glassborow, so I searched, and also found this list with recommended books.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jan 5th, 2006, 5:00 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
For this particular problem, use a while loop:
int i;
while ((std::cout << "Enter a number: ") && !(std::cin >> i))
{
    std::cout << "That's not a number, genius." << std::endl;
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC