944,221 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 947
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 2nd, 2009
0

How to make the cout work when input is not integer in this simple program?

Expand Post »
In the following very simple program, if the input is not an integer (e.g. a character symbol), then the output screen would flash and go away. Is there a way to have the output "bad" when one enters a non-integer, just as when one enters a negative number?
Thanks in advance!

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int x;
  4. cout << "enter integer: ";
  5. cin >> x;
  6. if (x>0)
  7. cout << "good";
  8. else
  9. cout << "bad";
  10. return 0;
  11. }
sgw
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
So, your wondering if you can input characters that are not integers? Sure you can. You can use char, getline, or string. I would suggest char if you are just starting to learn.

To allow the user to enter in more than one letter simply add an array to the char statement, as such:
char word[20];

This allows the user to enter and store 20 letters. Hope this was what you were asking for.
Reputation Points: 102
Solved Threads: 17
Posting Whiz in Training
restrictment is offline Offline
228 posts
since Oct 2009
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Hi, no, I don't mean how to input characters (I know you can do that if you declare x to be char type). What I'd like to know is, if I declared the variable x to be int type, and when running the program, if you entered a character/symbol (say "*", or "X"), then I want the screen to display the word "bad", rather than just flash away. In other words, I'd like there to be an output even when the input is an invalid type.



So, your wondering if you can input characters that are not integers? Sure you can. You can use char, getline, or string. I would suggest char if you are just starting to learn.

To allow the user to enter in more than one letter simply add an array to the char statement, as such:
char word[20];

This allows the user to enter and store 20 letters. Hope this was what you were asking for.
sgw
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
My usual advice is to always read all user input as a string. If you want a numeric value, attempt to perform a conversion. If the conversion fails, issue a message or something; otherwise you successfully obtained a numeric value, so continue.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Could you please show how exactly to do so in this particular program? (How to modify the code?) Thanks in advance.

My usual advice is to always read all user input as a string. If you want a numeric value, attempt to perform a conversion. If the conversion fails, issue a message or something; otherwise you successfully obtained a numeric value, so continue.
sgw
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Click to Expand / Collapse  Quote originally posted by sgw ...
Hi, no, I don't mean how to input characters (I know you can do that if you declare x to be char type). What I'd like to know is, if I declared the variable x to be int type, and when running the program, if you entered a character/symbol (say "*", or "X"), then I want the screen to display the word "bad", rather than just flash away. In other words, I'd like there to be an output even when the input is an invalid type.
Oh sorry, I must have read it too fast. >.<

Anyways, I agree with what Dave said. To implement this into the code just add the <string> header, and turn 'int' into 'string'.
Last edited by restrictment; Nov 2nd, 2009 at 6:28 pm.
Reputation Points: 102
Solved Threads: 17
Posting Whiz in Training
restrictment is offline Offline
228 posts
since Oct 2009
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Thanks a lot, both. However, it doesn't seem to solve my problem. It seems you answers (including the linked page) deal with only when x is changed to be a string. But what I want is, really, that x is supposed to be an integer, which can be used for computation for example, but on the other hand, when user enters a character, I'd like the program to tell him, for example, "your input must be a number, not character!" (or "bad" in the original code). And if the input was an integer, then the program will be able to continue with whatever it does with that integer (e.g. compare whether x>0, or do arithmetic operations...). If I use your solution and declare x to be a string, then "(x>0)" will not make sense and not even pass compiling.

So is there a way?

(by the way I tried cin.ignore and it didn't work)
Last edited by sgw; Nov 2nd, 2009 at 7:35 pm.
sgw
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
sgw is offline Offline
58 posts
since Jan 2008
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Click to Expand / Collapse  Quote originally posted by sgw ...
If I use your solution and declare x to be a string, then "(x>0)" will not make sense and not even pass compiling.
Well, if you don't get an integer because the user didn't enter a valid one, then "(x>0)" makes no sense either. You can do it your way by recognizing input failure and cleaning up the input stream. I believe there are sticky notes ("Read Me" items) where such things are described. Six of one, a half dozen of the other.
Last edited by Dave Sinkula; Nov 2nd, 2009 at 7:45 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 2nd, 2009
0
Re: How to make the cout work when input is not integer in this simple program?
Maybe this will help :

C++ Syntax (Toggle Plain Text)
  1. int x = 0;
  2. cin >> x; //get input, expecting an numeric value
  3. if(! cin ) //check if input wasn't a numeric value
  4. {
  5. cin.clear(); //clear the stream
  6. while(cin.get() != '\n') //discard all bad inputs up until the line end
  7. ;
  8.  
  9. cout<<"Bad\n"; //tell user the input is bad
  10. }
  11. else cout<<"good\n";
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help please
Next Thread in C++ Forum Timeline: operator= changes value just for scope of function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC