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?
Re: Determine if a string is composed of numbers or characters
You want ==, not = for comparison, but I'm not sure what the point of comparing anything to I is.
Re: Determine if a string is composed of numbers or characters
Quote:
Originally Posted by
GCDEF
You want ==, not = for comparison, but I'm not sure what the point of comparing anything to I is.
I get the same error, just more of them when I use ==.
The idea was to compare the first string character to the numbers 0,1,2,3...9 and if it matched any of them, the string is composed of numbers.
Re: Determine if a string is composed of numbers or characters
Quote:
Originally Posted by
HeliPilot
I get the same error, just more of them when I use ==.
The idea was to compare the first string character to the numbers 0,1,2,3...9 and if it matched any of them, the string is composed of numbers.
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.
Re: Determine if a string is composed of numbers or characters
Quote:
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
You'll need to show the code if you want help with errors.
Re: Determine if a string is composed of numbers or characters
Quote:
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
substr returns a string. isidigit wants a char.
Re: Determine if a string is composed of numbers or characters
Quote:
Originally Posted by
GCDEF
substr returns a string. isidigit wants a char.
Is there a way to pull out the first character of a string as a char? Or even convert string to char?
Re: Determine if a string is composed of numbers or characters
Quote:
Originally Posted by
HeliPilot
Is there a way to pull out the first character of a string as a char? Or even convert string to char?
You may want to look at the c_str() method of string.
Re: Determine if a string is composed of numbers or characters
... or something like that:
Code:
string s("qwerty");
isdigit(s[0]);
Re: Determine if a string is composed of numbers or characters
Thanks guys.
I just ended up using peek() to look at the first part of the string before actually pulling it out as a string.
Re: Determine if a string is composed of numbers or characters
Quote:
Originally Posted by
HeliPilot
I just ended up using peek()
What is this "peek" function? There is no such function defined in Visual C++.
Regards,
Paul McKenzie