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
 
0
  #11
Nov 3rd, 2009
It works! Thank you very much firstPerson!

Originally Posted by firstPerson View Post
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";
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
  #12
Nov 3rd, 2009
Hmmm, it works if one enters a character input, but not if one enters a float type (e.g. "2.4"): in the latter case the screen would again flash away, instead of displaying "Bad".

(by the way, of course I have slightly modified your code, so x is not assigned a value at the beginning but gets a value from input:
  1. int x;
  2. cout << "Enter an integer: ";
  3. cin >> x;
  4. ...


Originally Posted by sgw View Post
It works! Thank you very much firstPerson!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #13
Nov 3rd, 2009
If you need only whole value integers. Then use a typecast on float and use that whole value.
  1. float f = 12.33;
  2. int i = int( f ); //i = 12

Then work with 12
Last edited by firstPerson; Nov 3rd, 2009 at 3:13 am.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
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
  #14
Nov 3rd, 2009
Thanks, this is new to me, too. However, this is still not what I mean to want the program do (sorry for seeming stubborn, maybe there's no way anyway, but just in case...). To put it simply:

What I mean is to have a program which will use an integer x, whose value is entered as input (e.g. "What is your age?"), but when the user accidentally enters any non-integer (such as character "Q", float "8.72"), he will get a message on the screen "Wrong input entered", rather than having the screen just flash and then go dark. The last program suggested by you does solve part of the problem (entering character will result in an error message), but entering a float type ("53.97") will not produce the error message but instead have the screen flash and then go dark.

I wonder if there is a way to be able to deal with invalid input being not only character but also float and any other non-integer type. (If there is, it's probably complicated, seeing the one you suggested).





Originally Posted by firstPerson View Post
If you need only whole value integers. Then use a typecast on float and use that whole value.
  1. float f = 12.33;
  2. int i = int( f ); //i = 12

Then work with 12
Last edited by sgw; Nov 3rd, 2009 at 11:38 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #15
Nov 3rd, 2009
Originally Posted by sgw View Post
Thanks, this is new to me, too. However, this is still not what I mean to want the program do (sorry for seeming stubborn, maybe there's no way anyway, but just in case...). To put it simply:

What I mean is to have a program which will use an integer x, whose value is entered as input (e.g. "What is your age?"), but when the user accidentally enters any non-integer (such as character "Q", float "8.72"), he will get a message on the screen "Wrong input entered", rather than having the screen just flash and then go dark. The last program suggested by you does solve part of the problem (entering character will result in an error message), but entering a float type ("53.97") will not produce the error message but instead have the screen flash and then go dark.

I wonder if there is a way to be able to deal with invalid input being not only character but also float and any other non-integer type. (If there is, it's probably complicated, seeing the one you suggested).
Then make another test case for floating point,
  1. float temp = 0;
  2. int val = 0;
  3. cin >> temp;
  4. //check for non number character error
  5.  
  6. int val = int( temp ) ; //get the whole value
  7.  
  8. if( val < temp ) cout << "bad\n"; //val should equal temp


So do the error check when temp is not a numeric value, then do
another check to see if the number entered is a float or a integer.
The second test might not always work.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
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
  #16
Nov 3rd, 2009
Thanks. I combined your two solutions and modified the program as follows, and it works to my satisfaction so far . firstPerson, your unfailing help is really appreciated!
(So what started as a seemingly simple problem turned out to require a not-so-simple code.)

(By the way, what does "if (! cin)" mean exactly? I copied yours but not sure the exact function of it. Can you put the "!" in front of any statment?)

  1. int main()
  2. {
  3. float x;
  4. int i;
  5. cout << "How old are you? ";
  6. cin >> x; //get input, expecting a numeric value
  7. cin.ignore(20, '\n');
  8. if(! cin ) //check if input wasn't a numeric value
  9. {
  10. cin.clear(); //clear the stream
  11. while (cin.get() != '\n'); //discard all bad input up until the line ends
  12. cout << "Your age should be a number.\n"; //tell user the input is bad
  13. }
  14. else if (int(x)<x) //check if input was not an integer, e.g. of float type
  15. cout << "Your age must be a whole number.\n";
  16. else
  17. {
  18. cout<<"(Good valid input.)\n";
  19. i = int(x); //change x to integer type
  20. cout << "You are " << i << " years old.\n";
  21. }
  22. cin.get();
  23. return 0;
  24. }



Originally Posted by firstPerson View Post
Then make another test case for floating point,
Last edited by sgw; Nov 3rd, 2009 at 5:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,415
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: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #17
Nov 3rd, 2009
"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,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #18
Nov 3rd, 2009
>>(By the way, what does "if (! cin)" mean exactly? I copied yours but not sure the exact function of it. Can you put the "!" in front of any statment?)
<<

First realize that "cin" is a istream object, you might not understand what the means until you get to learn about class. Anyways, istream overloads the ! opertor so you could use it in conjunction with istream object.

So, when you do something like this :
  1. int x = 0;
  2. cin >> x;

Since x is a integer, and you are using cin to get a integer for an input,
then that means that cin is expecting a integer to be inputted. By if an
integer has not been inputted, cin sets off flags. These flags are just
bits. When a bit that handles fail in input is set, then that bit is set to
true and when everything is fine, that bit is false. So that means you
can use cin to check if certain bits has failed or not, and if so then whatever that bit represents( maybe failed input of end of file or
hardware failure), is set to true to indicate that something has gone
wrong. You can skip checking the bits and go straight to checking the istream object, cin using the ! operator. The ! operator when using like
this
  1. if ( ! cin ) { ... }
is read as if cin has failed meaning
that if something went wrong, like the user didn't input the type of value expected or memory failure, then go into the { ... } part.

That was a lot, hope you didn't get bored.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
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
  #19
Nov 3rd, 2009
Thanks David Sinkula for hte link (need some time to read the whole thing).

Thanks firstPerson for the explanation, no I'm not bored but glad to know something new. By the way is there a reason why you always assign x=0 before the cin >> x? If x is going to be input, why assign it 0 in the beginning? Perhaps there's something to it?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,362
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 173
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #20
Nov 3rd, 2009
Yea, to show that initializing variable should become a habit for it
will save bugs from appearing when working on bigger projects.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th 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