Let's analyse what your last posted program is doing. We start at the beginning of main. Memory has already been allocated for your statically-declared char array and int size_t, so the first thing that happens is to print the message and read the input into your char array. I guess you understand that so far...
At line 18, we invoke a function called strlen. Now, NORMALLY, this would invoke the strlen function declared in "string.h" that you have included. In your case, however, you have overridden this function with your own. So, when you call strlen on line 18, it jumps to line 28, sets the local variable size_t to 0, and returns it. It's essentially a useless function, because it always returns 0.
The program then continues to run from line 20.
What do you need to change? If you really want to do the calculation in a separate function, you can do it like this:
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
|
#include <iostream>
using namespace std;
// Prototype for your CUSTOM function
unsigned int myStringLengthFunction(const char * str);
int main()
{
char anywordstring[100];
int mysize;
cout<<"Please enter anyword\n";
cin.getline(anywordstring,100);
mysize=myStringLengthFunction(anywordstring);
cout<<"You enterd a word" << "\n" <<mysize<< " characters in length\n";
cin.get();
cin.get();
return 0;
}
// Implementation of your custom string calculation function
unsigned int myStringLengthFunction(const char * str)
{
return strlen(str);
}
|
Perhaps this will show you how functions work a bit more. However, be aware that the above code is rather overkill (i.e. you can just call strlen from your main method, you don't need to create your own function which calls it).