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;
}
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;
}
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.
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;
}
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
}
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;
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;
}