| | |
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
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!
Thanks in advance!
C++ Syntax (Toggle Plain Text)
int main() { int x; cout << "enter integer: "; cin >> x; if (x>0) cout << "good"; else cout << "bad"; return 0; }
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.
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.
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
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.
•
•
•
•
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.
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
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
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.
0
#6 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.
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.
0
#7 21 Days Ago
I think the basics are here:
http://www.daniweb.com/tutorials/tutorial71858.html
http://www.daniweb.com/tutorials/tutorial71858.html
"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
•
•
Join Date: Jan 2008
Posts: 42
Reputation:
Solved Threads: 0
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)
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.
0
#9 21 Days Ago
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
0
#10 21 Days Ago
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";
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?![]() |
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 desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






