![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2005
Posts: 33
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Thanks... That worked nicely!
Although there is something I don't understand. if(cin>>x) 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) |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2005
Posts: 33
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
Quote:
Thanks a lot for the information.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
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. |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#10 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|