| | |
Type Conversion of User Input:
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am working to add some simple user input checking into my program; there are three (3) int choices from a menu-- 1, 2, or 3. The user must choose one of these. I am able to check whether or not the input is 1, 2, or 3-- but I am not sure how to go about checking that the input is not possibly (accidently) a float or char. Any other input besides an int causes a failure and an infinite loop. I must prevent this.
I considered the idea of converting all input to int (from float or char) but I have had little sucess with my research with this so far, converting an input value to int from any possible other type (float, char, double, etc.)
Any advice, direction, or resources on this matter would be well appreciated. Thank-you in advance.
sharky_machine
I considered the idea of converting all input to int (from float or char) but I have had little sucess with my research with this so far, converting an input value to int from any possible other type (float, char, double, etc.)
Any advice, direction, or resources on this matter would be well appreciated. Thank-you in advance.
sharky_machine
c Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> using namespace std; long convert_to_int(const char* input ) { char* end = 0; long choice; choice = strtol( input, &end, 10 ) ; if ( *end != NULL ) { cout << "couldn't convert " << input << " to int\n" ; return 0; } else { cout << "Number " << choice << "\n" ; return choice; } } int main () { string input = "10"; convert_to_int(input.c_str()); input = "10.000"; convert_to_int(input.c_str()); input = "a"; convert_to_int(input.c_str()); return 0; }
バルサミコ酢やっぱいらへんで
•
•
•
•
I considered the idea of converting all input to int (from float or char) but I have had little sucess with my research with this so far, converting an input value to int from any possible other type (float, char, double, etc.)
Any advice, direction, or resources on this matter would be well appreciated. Thank-you in advance.

First of all, your input must be in char* or string. Then you must check each character entered to see if it's the correct type of value -- like all digits for int. If there's a bad character, decide what you need to do -- convert what you can or call it an error and try again.
If all is well, convert the string and return the proper value.
Last edited by WaltP; Nov 13th, 2006 at 3:45 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
c Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> using namespace std; long convert_to_int(const char* input ) { char* end = 0; long choice; choice = strtol( input, &end, 10 ) ; if ( *end != NULL ) { cout << "couldn't convert " << input << " to int\n" ; return 0; } else { cout << "Number " << choice << "\n" ; return choice; } } int main () { string input = "10"; convert_to_int(input.c_str()); input = "10.000"; convert_to_int(input.c_str()); input = "a"; convert_to_int(input.c_str()); return 0; }
when you get the chance, could you please explain this above code to me a bit in your own words?
Thank-you in advance.
sharky_machine
•
•
•
•
Are you sure you're ready for this? It's somewhat complicated
First of all, your input must be in char* or string. Then you must check each character entered to see if it's the correct type of value -- like all digits for int. If there's a bad character, decide what you need to do -- convert what you can or call it an error and try again.
If all is well, convert the string and return the proper value.
Thank-you for your reply and help.

Yes, I'm sure I'm ready to use this, I just need to understand it a bit better before I make use of it.
Thanks again,
sharky_machine
•
•
•
•
WolfPack:
, could you please explain this above code to me a bit in your own words?
convert_to_int that converts the string to int is the strtol function.From the documentation you see that it gives you the long value of a string. If there was a character that couldn't be converted, e.g. an alphabetic char, the value of
end will be that character. that is the meaning of •
•
•
•
end is set to point to whatever is left in start after the long.
end, and if it is not null, that means some character that was not conversible is in the chracter string. If that happens return an error and exit. You can even throw some kind of exception because this is C++.input.c_str() is how to get a non-modifiable pointer to the characters contained in it.Hope this explaination suffice.
Last edited by WolfPack; Nov 14th, 2006 at 12:48 pm.
バルサミコ酢やっぱいらへんで
![]() |
Similar Threads
- user input into a string (C++)
Other Threads in the C++ Forum
- Previous Thread: what is C++ builder code for sum
- Next Thread: Unable to correct
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






