Help with String Functions. almost got it...i think

I'm working on this string function but i cant get it to work. I almost have it but i'm getting these errors....
1 error C2065: 's' : undeclared identifier
2 error C2228: left of '.length' must have class/struct/union
3 warning C4390: ';' : empty controlled statement found; is this the intent?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <string>
#include <sstream>
#include <iostream>
using namespace std;
int NumDouble (string S) {
	int nd = 0;
	for (int nd=1; nd<s.length( ); nd++);
	{
		if (S[nd-1]==S[nd]);
	}
}
int main() {
	cout << "Enter a string: ";
	string q;
	cin >> q;
	int x;
	x = NumDouble(q);
	cout << "Number of doubles in" << q << " is " << x <<endl;
	system("pause");
}
line 7
look at line 5 then line 7

you might want to use unsigned nd instead of signed

line 15, use getline(cin, q) instead of cin>>. This is so that it doesn't skip any white space in the string
I'm getting these errors now.

Warning 1 warning C4390: ';' : empty controlled statement found; is this the intent?
Error 2 error C4716: 'NumDouble' : must return a value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int NumDouble (string S) {
	string s;
	int nd = 0;
	for(unsigned int nd=1; nd<s.length( ); nd++);
	{
		if (S[nd-1]==S[nd]);
	}
}
int main() {
	cout << "Enter a string: ";
	string q;
	getline(cin, q);
	int x;
	x = NumDouble(q);
	cout << "Number of doubles in" << q << " is " << x <<endl;
	system("pause");
}
You didn't return a value in your NumDouble function. Unless the function is void, it has to return a value. I'm guessing you will need a counter after the condition on line 10 to keep track of the number of doubles. Your function will not do what you want even after fixing what I just mentioned because of line 6; why do you need another string in the function when you are getting a string as your parameter?
this is a problem i have for the class and i cant figure it out. this is what the question says....

" Write a function called NumDouble that takes a string S as an argument
and returns the number of times there is a double-letter inside it.
For example if S=“happy” then the function returns 1.
If S=”balloon” or S=”Zaxxxon” the function returns 2.
If S=”Lollapallooza” the function returns 3… etc "
I'm a beginner and I'm having trouble with this and the professor doesn't know how to explain this properly.
Ok you are pretty much 80% done this function, you just have to:
1: create a new int varaible inside your function above the for-loop
2: Set this value to 0
3: Remove string s line 6
4: Remove the semicolon at line 10 and instead increment the variable you created
5: Return the variable you created by placing return nameofvaluehere; below the closing bracket of the for-loop


The reason for creating a new variable is so that you can count the number of times your if-statement (line 10) evaluates to true

The reason for not having a string in your function is so that you use the string S in your parameter

Reason for returning a value in your function is because that is the purpose of this project

gl
Your for loop has no statement(s), it just checks to see if x == y and then doesn't do anything. It is incomplete. That is why you are getting an error saying it does not return a value. The function must return an integer value or be declared as void.
Last edited on
Now it says string subscript out of range. when i took out the string s from line 6 i was still getting the same errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int NumDouble (string S) {
	int nd;
	int n=0;
	string s;
	for(unsigned int nd=1; nd<s.length( ); n++);
	{
		if (S[nd-1]==S[nd]);
	}
	return n;
}
int main() {
	cout << "Enter a string: ";
	string q;
	getline(cin, q);
	int x;
	x = NumDouble(q);
	cout << "Number of doubles in" << q << " is " << x <<endl;
	system("pause");
}
You still need a statement to go with that "if". "If x==y z=whatever, return z" would tie it up nicely. (sorry, being lazy).
how should i fix it??? im lost. (thank you so much for your help by the way i really do appreciate it. thank you)
You didn't take out string s, it is still there on line 8
Remove the semicolon after your for-loop on line 9 and after your if-statements on line 11

You declared variable nd 2wice line 6 and line 9

You are still not incrementing the value you set to zero.

Dude, get a piece of paper, write down the instructions the way you understand them and type it here. It seems you are a little scatter brained atm and you gotta focus on the task at hand
Last edited on
for(unsigned int nd=1; nd<s.length( ); n++);
{
if (S[nd-1]==S[nd]);
}
return n;

The format of an if statement is if (condition) statement;

You have if (condition);

Tell it to do something if S[nd-1]==S[nd] such as change the value of n.
Last edited on
Topic archived. No new replies allowed.