Strings as parameters in Functions

Hi Guys

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 :)

void comparing(string str1)
{
string str2;

if (str1.compare("0") != 0) {

cout << "Your string is " << str1.size() << " characters long." << endl << "Enter another string" << endl;
cin >> str2;
comparing (str2);
}

}

int main(){
string str3;
cout << "Enter a string" << endl;
cin >> str3;
comparing(str3);

system("pause");

return 0;
}

Thank you for your help. Much appreciated
some member functions of string.h

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

1
2
3
string str3;
cout << "Enter a string" << endl;
getline(cin, str3);
Last edited on
As Moschops said, the problem is the default behavior of cin, not the function. Though it's kinda problematic on it's own...

str1.compare("0") != 0 I don't know what you think it does, but I doubt it is the same as what's actually going on.

comparing (str2);

Is there any specific reason why you want to force a stack overflow here?
@ 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?


Thanks again
My intention was that until "0" is entered, the program keeps recursively running.


Why do you need it to recurse? Can you not just call the function repeatedly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<string>
#include <iostream>

using namespace std;

void comparing(string str1)
{
cout << "Your string is " << str1.size() << " characters long." << endl << "Enter another string" << endl;
}

int main(){
string str3;
while (1)
{
cout << "Enter a string" << endl;
getline(cin, str3);
 if (str3 == "0") break;
comparing(str3);
}
return 0;
}


I see that your program is much 'simpler'. thanks

Just one question though.As a beginner, I ask what exactly makes your method better than the one I used above?
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.
Thank you for that mate. Much appreciated.
Topic archived. No new replies allowed.