|
|
I'm not a C++ programmer, but do you have to close the parentheses in the if statement? Also, should the for statement be nChar < strString.Length()? I don't know what >> means, but I'm just guessing it is greater than.
|
Yeah I had to type it out cause it wouldn't copy for some reason so there's some random mistakes but the actual code is fine (well, apart from it doesn't work :X)
|
United States24493 Posts
Wait, couldn't you just return strString.length()? What exactly is going on lol.
When I did c++ I always used the AP Include files so I have no idea how to do string without apstring.h XD
|
micronesia brings up a good point... This function is completely useless. You could just use string.Length() instead of creating a method to do the exact same thing. EDIT: Just a random question, when using greater/less than signs, is it still shifters in C++ like in Java?
|
?
I don't want to include whitespaces (as I said in the op), so strString.Length() won't work
|
On July 21 2009 07:42 micronesia wrote: Wait, couldn't you just return strString.length()? What exactly is going on lol.
When I did c++ I always used the AP Include files so I have no idea how to do string without apstring.h XD He's trying to get the length minus the whitespace I guess.
|
In any case, since you say that string isn't a valid parameter, my guess is that you have forgotten to include string.h, or to use the std namespace.
|
Not sure about your function since it's been so long since I did c++ but you should be able to google for c++ string function arguments to find the correct way.
On another note, currently you are only getting rid of the ' ' (spaces) in your count, and not any whitespace such as a tab. Might want to look into that as well.
|
(not including whitespace)
I don't want to include whitespaces
He's trying to get the length minus the whitespace I guess.
|
Still, the actual length of the string and the string doesn't change, so the returned value from strString.length() will be constant throughout the entire loop no matter how many whitespaces. Just return strString.length() to a nSomething and use nChar < nSomething in the loop. I can't see why this wouldn't work?
To clarify, say your string is "HI MOM".
You get the length of the string which is 6 characters. You loop through, count the H, nLength ++1; Loop through, count the I, nLength ++1; Loop through, won't count the whitespace. Loop through, counts the M, nLength ++1; Loop through, counts the O, nLength ++1; Loop through, counts the M, nLength ++1;
At the end of this, you will have the length of the actual string which will still be 6 characters, and the nLength variable which you individually use to count the characters except for the whitespace which will be 5.
|
What IDE are you using? I loaded your code up into Visual Studios 2008 and it works fine. http://pastebin.com/m1fe7c353
Results in
are you making sure you include iostream and using namespace std?
|
Ok after a quick google I found some code using this syntax
void function (std::string &str) { }
|
Oh, I see what you mean now! If you want to ignore tabs, you may consider using the C++ equivalent of the Java: strString[nChar] != ' ' && !(strString[nChar]+"").equals("\t"); The second part is converting the character to a string, to use .equals() function (in Java, Strings don't use ==), and comparing it to the string of a tab (\t). But other than that, I can't really see an obvious problem. You may consider issues not within the code, but within your compiler.
|
Make sure you're compiling the correct up to date version of the code and using the updated executable. Silly thing to point out but this is what cause several pages of confused people in konadora's blog asking for programming help.
|
I didn't realise I needed to add using namespace std; before the function, I thought I could just add it in int main() where the function would get called because that's when it would be run first..
Thanks
|
I'm glad I (we) could help! Good luck learning C++! It's a rewarding experience.
|
Croatia9454 Posts
I'm a C programmer, and I'm just wondering did C++ abandon pointers completely or can you still use them?
|
yea you can use pointers in C++.
|
Croatia9454 Posts
Oh ok. And sorry for taking over the thread, but what's with this string data type? What's the difference between string type and char[]?
Even though I prefer C over all other programs, I'm gonna have to do some C++ for uni this year so just getting prepared I guess...
|
|
|
|