943,634 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1913
  • C++ RSS
Feb 5th, 2009
0

Checking if a string is an unsigned integer

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jraven1 is offline Offline
1 posts
since Feb 2009
Feb 5th, 2009
0

Re: Checking if a string is an unsigned integer

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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 6th, 2009
0

Re: Checking if a string is an unsigned integer

Maybe something like this comes helpful:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Feb 6th, 2009
0

Re: Checking if a string is an unsigned integer

*Mumbles something about multiple exit points*
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 7th, 2009
0

Re: Checking if a string is an unsigned integer

You can do that:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Feb 9th, 2009
0

Re: Checking if a string is an unsigned integer

I think you can simply check if the first character is a numeral.
Last edited by kbshibukumar; Feb 9th, 2009 at 10:01 am.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Feb 9th, 2009
0

Re: Checking if a string is an unsigned integer

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 9th, 2009
0

Re: Checking if a string is an unsigned integer

c++ Syntax (Toggle Plain Text)
  1. inline bool isUint(const std::string& s)
  2. {
  3. return s.find_first_not_of("0123456789") == std::string::npos;
  4. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Calculating Error Gradient in a Neural Network
Next Thread in C++ Forum Timeline: Operator overloading.operator loading?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC