I started c++ a couple of days ago and was going over some member functions of string.h
This below is the function (comparing) I used in my program whose aim was that when a string is inserted it gives out the number of characters in the string
The problem arose as every time a string which included a 'space' character, the function below divided the string in two separate strings. I recon that the problem is in the use of the string as a parameter in the function... Can anyone help please :)
I presume you mean <string>? string.h is C code, and C doesn't do member functions. If you did mean string.h, then you've got some ancient compiler and you need to get a new one for free that uses correct, modern C++.
Anyway, that's how cin works. If you want to include spaces, try
@ Moschops Yes I was refering to <string> sorry.... I was reading other forums, and were using string.h instead... As I said I'm still new to c and c++ but thanks
The problem is fixed... It's all due to the use of cin instead of getline.
@ hanst99
I think that the name 'comparing' wasn't quite adequate in this case. My intention was that until "0" is entered, the program keeps recursively running. Do you suggest a better way to doing this?
Your method was recursive for no good reason. When you call a function, some memory is used in a region known as the "stack". When the function ends, that memory is available again for the next function. When you call a function from inside another function, it uses a bit more of the stack; the bit used for the first function ( which of course needs to remain in existence so that when the second function returns all the variables are still there and the next code to execute is ready) and on top of that the bit for the next function.
When you call a function from inside a function from inside a function, you've now used more of the stack. And so on, and so on. Since your code called comparing inside comparing inside comparing inside comparing inside comparing inside comparing inside comparing inside comparing and so on forever and ever, it uses more and more and more of the stack until eventually there is no stack left and the program crashes.