| | |
Checking if a string is an unsigned integer
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
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?
Thanks
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)
bool checkUnsignedInt(string *str) { unsigned long value; stringstream ss(*str); if (ss >> value) { return true; } else { return false; } }
Thanks
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Maybe something like this comes helpful:
C++ Syntax (Toggle Plain Text)
bool checkUnsignedInt(string *str) { unsigned long value; stringstream ss(*str); if (ss >> value) { string str; ss >> str; return str.empty(); // nothing following the digits } return false; }
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
Please use code tags - Please mark solved threads as solved
You can do that:
C++ Syntax (Toggle Plain Text)
bool checkUnsignedInt(const string &str) { return atoi(str.c_str()) >= 0; }
Last edited by minas1; Feb 7th, 2009 at 4:56 am.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
I think you can simply check if the first character is a numeral.
Last edited by kbshibukumar; Feb 9th, 2009 at 10:01 am.
c++ Syntax (Toggle Plain Text)
inline bool isUint(const std::string& s) { return s.find_first_not_of("0123456789") == std::string::npos; }
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- Command Line Arguement, can't get it to read in an int (C)
- C serial com port terminal program (C)
- CRC Calculation (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Calculating Error Gradient in a Neural Network
- Next Thread: Operator overloading.operator loading?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






