Checking if a string is an unsigned integer

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: jraven1 is an unknown quantity at this point 
Solved Threads: 0
jraven1 jraven1 is offline Offline
Newbie Poster

Checking if a string is an unsigned integer

 
0
  #1
Feb 5th, 2009
Hi,

I'm having problems checking whether a string is an unsigned integer. The code I am using only checks the first character of the string, so "a1" would return false, but "1a" would return true. Any ideas where I'm going wrong?

  1. bool checkUnsignedInt(string *str)
  2. {
  3. unsigned long value;
  4. stringstream ss(*str);
  5.  
  6. if (ss >> value)
  7. {
  8. return true;
  9. }
  10. else
  11. {
  12. return false;
  13. }
  14. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Checking if a string is an unsigned integer

 
0
  #2
Feb 5th, 2009
What exactly is the problem? this works fine for me....nvm

EDIT:
of you need to loop through each character rather than just using the first one

Chris
Last edited by Freaky_Chris; Feb 5th, 2009 at 1:31 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Checking if a string is an unsigned integer

 
0
  #3
Feb 6th, 2009
Maybe something like this comes helpful:

  1.  
  2.  
  3. bool checkUnsignedInt(string *str)
  4. {
  5. unsigned long value;
  6. stringstream ss(*str);
  7.  
  8. if (ss >> value)
  9. {
  10. string str;
  11. ss >> str;
  12. return str.empty(); // nothing following the digits
  13. }
  14. return false;
  15. }
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Checking if a string is an unsigned integer

 
0
  #4
Feb 6th, 2009
*Mumbles something about multiple exit points*
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Checking if a string is an unsigned integer

 
0
  #5
Feb 7th, 2009
You can do that:

  1. bool checkUnsignedInt(const string &str)
  2. {
  3. return atoi(str.c_str()) >= 0;
  4. }
Last edited by minas1; Feb 7th, 2009 at 4:56 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: Checking if a string is an unsigned integer

 
0
  #6
Feb 9th, 2009
I think you can simply check if the first character is a numeral.
Last edited by kbshibukumar; Feb 9th, 2009 at 10:01 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Checking if a string is an unsigned integer

 
0
  #7
Feb 9th, 2009
Originally Posted by kbshibukumar View Post
I think you can simply check if the first character is a numeral.
Thats what his code does, but that doesn't mean that "1dfjkghdflgh" is an integer, because it isn't. But by your logic it would be.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Checking if a string is an unsigned integer

 
0
  #8
Feb 9th, 2009
  1. inline bool isUint(const std::string& s)
  2. {
  3. return s.find_first_not_of("0123456789") == std::string::npos;
  4. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC