|
|
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 States24686 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.
|
Croatia9505 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++.
|
Croatia9505 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...
|
char[] is the C way and string is the C++ way a string is pretty much a character array.
|
use (const char* string) in your parameter?
|
Sanya12364 Posts
You should definitely pass by reference.
int Characters(const std::string &strString); is much preferred.
Here you aren't going to run a copy constructor on the string on every call. The const also tells the compiler that you aren't going to change any of the contents of the obj during the call.
nvm the edit.
|
|
look, the thing is simple, the namespaces are wrong.
You can do either:
# # using namespace std;
after every include to set std for every variable Or
std::string
any time you define a string variable.
This is because the namespace can be different, you could use other Library sets or the same with other names on the Classes.
In the default set of librarys you should put like header of your main
#include<string.h> using namespace std;
And then your function will compile with no problem.
|
On July 21 2009 08:49 2Pacalypse- wrote: I'm a C programmer, and I'm just wondering did C++ abandon pointers completely or can you still use them?
Nono, advanced c++ is all about pointers... is much better for making some vector type or list type classes.
|
On July 21 2009 07:51 GogoKodo wrote: Ok after a quick google I found some code using this syntax
void function (std::string &str) { }
This one includes a "&" which passes the argument by reference. It changes the value of the parameter. Although the program won't change the string, it is unnecessary here.
|
Sanya12364 Posts
Unnecessary, but still good style. Passing by value implicitly triggers the copy constructor on call and a destructor on exit. Passing by reference does neither.
If the function is used with any frequency, you'll copy strings and destroy strings over and over again. It'll be slow. Classes are not to be passed by value unless absolutely necessary. Start practicing good programming style on the simple examples and it'll come naturally later.
|
On July 21 2009 11:20 coltrane wrote: Nono, advanced c++ is all about pointers... is much better for making some vector type or list type classes.
I program "advanced" C++ and I don't use pointers. Well, not bare pointers. Manly C++ is done using Boost, and therefore boost::shared_ptr. I have an infrequently updated blog about this stuff.
On July 21 2009 09:08 2Pacalypse- wrote: And sorry for taking over the thread, but what's with this string data type? What's the difference between string type and char[]?
C++ stuff.
First, the full name is "std::string". The "std" part is a namespace. Names in C++ can be put into separate namespaces, so that you don't have type name collisions. Like if you have one library that defines a type called "string", it can put it in the namespace "library". Thus std::string and library::string can live and function within the same program.
Second, std::string is a C++ Class. It's sort of like a struct in C, but a lot more intelligent. On the creation of a Class, you are guaranteed that a certain function will be called. This function, the constructor, allows you to do initialization work, which ensures that you never use uninitialized objects. Similarly, when the object is destroyed, another function is called that allows you to do cleanup work. Also, Classes can have functions in them that operate on the given instance of that class.
std::string is an intelligent wrapper around a char* string. It's constructor allocates memory for it. If you start adding characters beyond the length of the allocated segment, it will expand the allocated region to enclose them all. It has its own searching and comparison functions. And because its destructor will handle the deletion of the internal char*, it can never leak memory (unless you fail to destroy the std::string, in which case you leaked memory). It is in every way superior to a naked char*.
|
manly c++ is done without ++
-_-;;
|
On July 21 2009 11:20 coltrane wrote:Show nested quote +On July 21 2009 08:49 2Pacalypse- wrote: I'm a C programmer, and I'm just wondering did C++ abandon pointers completely or can you still use them?
....
Why can't you just use STL libaries for them?
|
On July 21 2009 13:08 ven wrote: manly c++ is done without ++
-_-;;
I've written straight-C before; that's just self-inflicted torture. Real men don't do things the hard way; they do them the smart way.
|
Germany2896 Posts
use "const string&" for string parameters you don't change
|
A char[] is a piece of memory, and it's up to you to make sure that this is a good string, which is usually some text followed by a zero. When using this, you will always have to keep the zeros in mind, and allocating your own memory, and just being very focused in general. You'll find yourself doing a lot of malloc/strncpy/strcat, pointer arithmetic and suchlike. It's a hassle, for newcomers and veterans alike. These manual labour strings even harder to do right in C++ than in C, what with exceptions and all.
An std::string takes care of the memory, allocating and releasing as needed. In addition, it is also usable as an STL container. I realize as I refresh the thread that the memory argument has been made a few times already. So I'll move on (just adding that I also support passing the string as a constant refererence).
As for the OP, the string class you want is std::string, and to use it you must include the string header (#include <string>, NOT <string.h>, string.h might work in some cases, but it is not standard C++). It is true that "using namespace std" solves your immediate problem, but you might end up with bad habits. This is explained here: http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.5
The C++ FAQ Lite is something I would heavily recommend any c++ programmer to keep in their bookmarks. It is actually quite pragmatic and attempts to explain things instead of just blurting out truths.
As for testing for whitespace, writing your own test can be good if you know exactly what you want. But I'm going to throw it out here that there is a function called std::isspace accessible through the ctype header (#include <ctype>) that tests for all the usual whitespace characters (' ', '\t', '\f', '\v', '\r', '\n').
As for whether C++ uses pointers or not, the answer is: yes, it does. Not using them would not be efficient. Some people who has used them enough learn that they are a lot of manual labour, however, and starts to find ways around the hardships. Pointers are dumb and they can become invalid without feedback (if someone else deletes it), or you can forget to delete it yourself (memory leaks and strange behavior). Smart pointers is a collective name for objects that try to manage pointers for you. boost::shared_ptr (http://www.boost.org/) (as pointed to earlier) is a good example of such a pointer, and is due to be included in the next c++ standard. But this is an extensive subject.
Sorry for re-iterating a lot of others' posts .
|
|
|
|