Returning number of characters without using strlen

Hey guys. Ive ran into a little bit of a problem but have a hard time getting around it. I have to create a c++ program counting the numbers characters in a string but without using strlen. My code after it compiles gives me this error which I dont know how to fix. I have provided my code beneath the error. If anyone could give me some suggestions it would be greatly appreciated. Hope to hear from you. Thanks

"Error 1 error LNK2019: unresolved external symbol "char __cdecl StringLength(char * const)" (?StringLength@@YADQAD@Z) referenced in function _main c:\Users\TONY X\documents\visual studio 2010\Projects\strlen not working\strlen not working\please work.obj"

Error 2 error LNK1120: 1 unresolved externals c:\users\tony x\documents\visual studio 2010\Projects\strlen not working\Debug\strlen not working.exe 1



#include <iostream>
using namespace std;

char StringLength(char []);

int main()
{
char input[80];
int len = 0;
cout << "Input a string: ";
cin >> input;


len = StringLength(input);
cout << "Length = " << len << endl;

return 0;
}


int StringLength (char x)
{
int counter = 0, num = 0;
for (int index = 0; index < 80; index++)
{
if (x != '\0')
counter ++;
num = counter;
}
return num;
}
1
2
char StringLength(char []); 
int StringLength (char x)


Notice anything different about these?
Yes I do. Thanks for catching that. I changed the "char" to "int" in the prototype. Ran the code again it still gives me the same error though.

#include <iostream>
using namespace std;

int StringLength(char []);

int main()
{
char input[80];
int len = 0;
cout << "Input a string: ";
cin >> input;


len = StringLength(input);
cout << "Length = " << len << endl;

return 0;
}


int StringLength (char x)
{
int counter = 0, num = 0;
for (int index = 0; index < 80; index++)
{
if (x != '\0')
counter ++;
num = counter;
}
return num;
}







1
2
int StringLength(char []); 
int StringLength(char x);


Notice anything different about these?
Thanks. I do see what I was doing wrong. In the prototype I had "char[]" and in the function header I had "char x" which should have been "char x[]". Ok so got past that part. My only issue now is when I do input my string it seems to be returning a value of 80 regardless of what I input. I know its most likely an issue with the logic I'm using. I thought using the "if statement" and setting the counter to increment each time a character does equal the null character but it seems to be counting the null characters regardless.

Ive tried changing what I had in my original code to this but still does not work. Please let me know if you guys have any other suggestions. Thanks Cire and Caligulaminus for your input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream> 
using namespace std; 

char StringLength(char []); 

int main() 
{ 
char input[80];
int len = 0;
cout << "Input a string: ";
cin >> input;


len = StringLength(input);
cout << "Length = " << len << endl;

return 0; 
}


int StringLength (char x[])
{
int counter = 0, num = 0;
for (int index = 0; index < 80; index++)
{
if (x != '\0')
counter ++;
num = counter;
}
return num;
}
In line 26 you are comparing the whole string to '\0'. You want to instead compare each element of the string (x[index] != '\0')
Last edited on
I had tried that before but it didnt make a difference in the result. I went ahead and changed it again. Here is my updated code for the function StringLength. I just cant seem to figure out why its still returning a higher value if in my "if statement" I'm only incrementing the elements that do not equal the null character. This time its returning 79 unlike last time which was 80.

1
2
3
4
5
6
7
8
9
10
11
int StringLength (char x[])
{
	int counter = 0, num = 0;
	for (int index = 0; index < 80; index++)
	{
		 if (x[index] != '\0')
			counter ++;
			num = counter;
	}
	return num;
}

Ok, I see the problem.

The for loop will continue, regardless of whether or not the condition in the if statement is true or not.

try throwing in an else statement which will end the function by returning the count:

1
2
3
4
5
6
7
8
9
if (x[index] != '\0')
{
			counter ++;
}
else
{
     counter++; //to count the '\0' as a character in the array
     return counter + 1; //+1 because the index starts at 0
}

Last edited on
1
2
3
4
5
6
	for (int index = 0; index < 80; index++)
	{
		 if (x[index] != '\0')
			counter ++;
			num = counter;
	}


This for loop is a bit off the mark - for (int index = 0; index < 80; index++) - it will always run until index is 80 because you do not break the loop when you find the '\0' - think about it.


EDIT
once you sort that out we can incorporate some error checking stuff;
Last edited on
Not sure if im on the right track but in my for loop instead of using "index < 80" I switched it to "index != '\0'. Is this something along the lines to which you were referring to guestgulkan?

1
2
3
4
5
6
7
8
9
10
11
int StringLength (char x[])
{
	int counter = 0, num = 0;
	for (int index = 0; index != '\0'; index++)  // Here is the change made
	{
		 if (x[index] != '\0')
			counter ++;
			num = counter;
	}
	return num;
}
The body of that for loop will never execute. '\0' != 0 is false.

You don't know how many times you need to iterate, which makes the choice of a for loop questionable. I'd use a while loop, myself.
Forgot to post my end result for my code here. Thanks everyone for the help its greatly appreaciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream> 
using namespace std; 

int StringLength(char []);

int main() 
{ 
	char input[80];
	int len;
	cout << "Input a string: ";
	cin >> input;


	len = StringLength(input);
	cout << "Length = " << len << endl;

	return 0; 
}


int StringLength (char x[])
{
	int counter = 0;
	for (int index = 0; index < 80; index++)
	{
		if (x[index] == '\0')
			break;
			counter ++;
		
	}
	return counter;
}



I never thought that this task is such hard.:)

1
2
3
4
5
6
7
8
unsigned int StringLength( const char s[] )
{
	unsigned int counter = 0;

	while ( *s++ ) counter++;

	return counter;
}
Topic archived. No new replies allowed.