-
December 7th, 2009, 02:57 PM
#1
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 03:05 PM.
-
December 7th, 2009, 03:01 PM
#2
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.
-
December 7th, 2009, 03:05 PM
#3
Re: Determine if a string is composed of numbers or characters
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.
-
December 7th, 2009, 03:13 PM
#4
Re: Determine if a string is composed of numbers or characters
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.
-
December 7th, 2009, 03:22 PM
#5
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'
-
December 7th, 2009, 03:31 PM
#6
Re: Determine if a string is composed of numbers or characters
You'll need to show the code if you want help with errors.
-
December 7th, 2009, 03:35 PM
#7
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'
-
December 7th, 2009, 03:35 PM
#8
Re: Determine if a string is composed of numbers or characters
substr returns a string. isidigit wants a char.
-
December 7th, 2009, 03:43 PM
#9
Re: Determine if a string is composed of numbers or characters
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?
-
December 7th, 2009, 03:44 PM
#10
Re: Determine if a string is composed of numbers or characters
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.
-
December 7th, 2009, 04:11 PM
#11
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:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
December 7th, 2009, 04:34 PM
#12
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.
-
December 7th, 2009, 06:58 PM
#13
Re: Determine if a string is composed of numbers or characters
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|