Type Conversion of User Input:

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Type Conversion of User Input:

 
0
  #1
Nov 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Type Conversion of User Input:

 
0
  #2
Nov 13th, 2006
C or C++?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Type Conversion of User Input:

 
0
  #3
Nov 13th, 2006
Originally Posted by WolfPack View Post
C or C++?
WolfPack:

Sorry about that. I am writing in C++.

Thanks.

sharky_machine
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Type Conversion of User Input:

 
0
  #4
Nov 13th, 2006
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. using namespace std;
  5. long convert_to_int(const char* input )
  6. {
  7. char* end = 0;
  8. long choice;
  9. choice = strtol( input, &end, 10 ) ;
  10. if ( *end != NULL )
  11. {
  12. cout << "couldn't convert " << input << " to int\n" ;
  13. return 0;
  14. }
  15. else
  16. {
  17. cout << "Number " << choice << "\n" ;
  18. return choice;
  19. }
  20. }
  21. int main ()
  22. {
  23. string input = "10";
  24. convert_to_int(input.c_str());
  25. input = "10.000";
  26. convert_to_int(input.c_str());
  27. input = "a";
  28. convert_to_int(input.c_str());
  29. return 0;
  30. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Type Conversion of User Input:

 
0
  #5
Nov 13th, 2006
Originally Posted by sharky_machine View Post
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.
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.
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Type Conversion of User Input:

 
0
  #6
Nov 13th, 2006
Originally Posted by WolfPack View Post
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. using namespace std;
  5. long convert_to_int(const char* input )
  6. {
  7. char* end = 0;
  8. long choice;
  9. choice = strtol( input, &end, 10 ) ;
  10. if ( *end != NULL )
  11. {
  12. cout << "couldn't convert " << input << " to int\n" ;
  13. return 0;
  14. }
  15. else
  16. {
  17. cout << "Number " << choice << "\n" ;
  18. return choice;
  19. }
  20. }
  21. int main ()
  22. {
  23. string input = "10";
  24. convert_to_int(input.c_str());
  25. input = "10.000";
  26. convert_to_int(input.c_str());
  27. input = "a";
  28. convert_to_int(input.c_str());
  29. return 0;
  30. }
WolfPack:
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Type Conversion of User Input:

 
0
  #7
Nov 13th, 2006
Originally Posted by WaltP View Post
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.
WaltP:
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Type Conversion of User Input:

 
0
  #8
Nov 14th, 2006
Originally Posted by sharky_machine View Post
WolfPack:
, could you please explain this above code to me a bit in your own words?
the heart of the function 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.
. So once the convertion is finished, you check the value of 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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Type Conversion of User Input:

 
0
  #9
Nov 14th, 2006
WolfPack:

Thank-you for you reply and explanation. It makes perfect sense to me now.

sharky_machine
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC