| | |
How to make the cout work when input is not integer in this simple program?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
0
#11 24 Days Ago
It works! Thank you very much firstPerson!
•
•
•
•
Maybe this will help :
C++ Syntax (Toggle Plain Text)
int x = 0; cin >> x; //get input, expecting an numeric value if(! cin ) //check if input wasn't a numeric value { cin.clear(); //clear the stream while(cin.get() != '\n') //discard all bad inputs up until the line end ; cout<<"Bad\n"; //tell user the input is bad } else cout<<"good\n";
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
0
#12 24 Days Ago
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:
(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:
C++ Syntax (Toggle Plain Text)
int x; cout << "Enter an integer: "; cin >> x; ...
0
#13 24 Days Ago
If you need only whole value integers. Then use a typecast on float and use that whole value.
Then work with 12
C++ Syntax (Toggle Plain Text)
float f = 12.33; int i = int( f ); //i = 12
Then work with 12
Last edited by firstPerson; 24 Days Ago at 3:13 am.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
0
#14 24 Days Ago
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).
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).
•
•
•
•
If you need only whole value integers. Then use a typecast on float and use that whole value.
C++ Syntax (Toggle Plain Text)
float f = 12.33; int i = int( f ); //i = 12
Then work with 12
Last edited by sgw; 24 Days Ago at 11:38 am.
0
#15 24 Days Ago
•
•
•
•
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).
C++ Syntax (Toggle Plain Text)
float temp = 0; int val = 0; cin >> temp; //check for non number character error int val = int( temp ) ; //get the whole value 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.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
0
#16 24 Days Ago
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?)
(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?)
C++ Syntax (Toggle Plain Text)
int main() { float x; int i; cout << "How old are you? "; cin >> x; //get input, expecting a numeric value cin.ignore(20, '\n'); if(! cin ) //check if input wasn't a numeric value { cin.clear(); //clear the stream while (cin.get() != '\n'); //discard all bad input up until the line ends cout << "Your age should be a number.\n"; //tell user the input is bad } else if (int(x)<x) //check if input was not an integer, e.g. of float type cout << "Your age must be a whole number.\n"; else { cout<<"(Good valid input.)\n"; i = int(x); //change x to integer type cout << "You are " << i << " years old.\n"; } cin.get(); return 0; }
Last edited by sgw; 24 Days Ago at 5:13 pm.
0
#17 24 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
0
#18 24 Days Ago
>>(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 :
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 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.
<<
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 :
C++ Syntax (Toggle Plain Text)
int x = 0; 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
C++ Syntax (Toggle Plain Text)
if ( ! cin ) { ... }
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.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
0
#19 24 Days Ago
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?
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?
0
#20 24 Days Ago
Yea, to show that initializing variable should become a habit for it
will save bugs from appearing when working on bigger projects.
will save bugs from appearing when working on bigger projects.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?![]() |
Similar Threads
- Mystery with getline, very simple program (C++)
- Simple Input/Output (BufferedReader) Program help (Java)
- Need help resolving errors for a simple program (Java)
- Need help for simple program, don't have a clue about C (C)
- Erroneus outputs in string manipulation simple program (C++)
Other Threads in the C++ Forum
- Previous Thread: help please
- Next Thread: operator= changes value just for scope of function
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






