Determine if a string is composed of numbers or characters
I have pulled a string from an input, and I have to execute a function on it depending on if the string is numbers or characters. I tried making a function file that can determine it, however I am getting an illegal expression error C2451:
Code:
int chooseMethod (string testStr){
for (int I = 0; I<10; I++){
if (testStr.substr(1,1) = I){
return 2;}}
return 1;
}
Then based on the return I use the corresponding function. The error lies within
Code:
if (testStr.substr(1,1) = I)
. What am I doing wrong, or is there a common/easier way to do this?
Last edited by HeliPilot; December 7th, 2009 at 02:05 PM.
Re: Determine if a string is composed of numbers or characters
Originally Posted by GCDEF
You want to compare every character, not just the first to determine if it's composed of numbers.
You could use the isdigit() function, or check if each char is >= 0 and <= 9.
Well since this is with an ideal input, if the first character is a number, then they will all be numbers. So would I still have to go through every character?
I tried the >=0 and <=9 and it just gives me more errors on build:
Code:
could not deduce template argument for 'const _Elem *' from 'int'
Re: Determine if a string is composed of numbers or characters
Originally Posted by GCDEF
You'll need to show the code if you want help with errors.
This is after attempting isdigit():
Code:
int chooseMethod (string testString){
if (isdigit(testString.substr(1,1))){
return 1;}
return 2;
}
Error:
Code:
1>c:\documents and settings\asdfasdf\desktop\example.cpp(325) : error C2664: 'isdigit' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'
Re: Determine if a string is composed of numbers or characters
... or something like that:
Code:
string s("qwerty");
isdigit(s[0]);
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
Bookmarks