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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster

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

 
0
  #1
21 Days Ago
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #2
21 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster
 
0
  #3
21 Days Ago
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.



Originally Posted by restrictment View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
21 Days Ago
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster
 
0
  #5
21 Days Ago
Could you please show how exactly to do so in this particular program? (How to modify the code?) Thanks in advance.

Originally Posted by Dave Sinkula View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #6
21 Days Ago
Originally Posted by sgw View Post
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; 21 Days Ago at 6:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #7
21 Days Ago
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 42
Reputation: sgw is an unknown quantity at this point 
Solved Threads: 0
sgw sgw is offline Offline
Light Poster
 
0
  #8
21 Days Ago
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; 21 Days Ago at 7:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #9
21 Days Ago
Originally Posted by sgw View Post
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; 21 Days Ago at 7:45 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,091
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 140
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #10
21 Days Ago
Maybe this will help :

  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";
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this equal :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC