Determining whether input is string or integer
-
Klazen wrote:
Is there any way around this
Several options come to mind. 1) Re-read the material provided for your course 2) Ask your instructor for some guidance 3) Network with other students in your class 4) Drop the course and change your Major
led mike
-
I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?
Klazen wrote:
Is there any way around this whithout simply telling the user to be careful and only enter numbers?
int x;
std::cin >> x;"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Klazen wrote:
Is there any way around this whithout simply telling the user to be careful and only enter numbers?
int x;
std::cin >> x;"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
That's what I am doing (well actually, I've got "using namespace std" and the top of the file, so I don't have to write that all over the place.) But when I type "s", for instance, it skips the rest of the "cin" statements and moves on to the code after it.
-
I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?
I'd recommend Stroustrup's book on C++ (title "The C++ Programming Language"), one of his first examples is a calculator which reads numbers and expressions from the input and shows one way to parse it and extract numbers, symbols, labels etc..
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
That's what I am doing (well actually, I've got "using namespace std" and the top of the file, so I don't have to write that all over the place.) But when I type "s", for instance, it skips the rest of the "cin" statements and moves on to the code after it.
Did you see this?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?
have a look at boost's lexical cast[^] The underlying implementation uses string streams (which is pretty slow) so you could do something similar yourself if you dont want to use their library something along the lines of this...
stringstream ss; string input; int value; try { ss << input; ss >> value; } catch(cant remember exactly what is thrown) { //if you get here its not an int }
-
have a look at boost's lexical cast[^] The underlying implementation uses string streams (which is pretty slow) so you could do something similar yourself if you dont want to use their library something along the lines of this...
stringstream ss; string input; int value; try { ss << input; ss >> value; } catch(cant remember exactly what is thrown) { //if you get here its not an int }
start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements
-
start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements
Gregory Bryant wrote:
start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements
:omg: What ?? Are you kidding ? You mean you want to use a static_cast on the char array to convert it to an integer ? Oh man, that is just plain wrong.
Cédric Moonen Software developer
Charting control [v1.2] -
Thanks for the sarcasm, but it's not an assignment. I'm simply trying to learn more about C++. Also, just FYI, I'm still in high school. Haven't even chosen a major.
-
start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements
Please don't answer questions until you know the answers. This is just plain wrong on so many levels.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )