What compiler are you using?
I can't see any error messages in your post.
Why int pointVal (string s) and where is int HowEasy::pointVal(string s) MEMBER FUNCTION definition? Probably, it's a reason of your troubles...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Wrong outputs... of a compiler, of a linker, of a program?..
Are you a pilot of a shot down F-117 under hostile cross-examination?..
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
ArkM is correct. It needs to be int HowEasy::pointVal(string s) . If you get other errors with that, the solution is to fix those errors, not go back to int pointVal(string s) . Try taking the ".h" off of your #include statements . Add return 0; to the end of the main function. What compiler/linker errors do you get? I don't get any after making those changes.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Thanks a lot prabhakar. I forgot to mention what the code is for. Forgive me as this is my first post. This program is for calculating the number of letters from words with only alphabets and the dividing it by the number of words. According to this value, the points are decided as in the loop(250,500,1000). And can you explain the #define isaplha () a bit. I know #define is for defining constants or some functions. But i have some trouble comprehending this. How can the alphabets alone suffice without using their ascii values?
While this works fine,
#define isalpha(a) (((a)>='A' && (a) <= 'Z') ||((a)>='a' && (a) <= 'z'))
I believe that the function below is preferable, particularly for someone new to C++. The above is probably more efficient due to the fact that you'll have fewer function calls, but is harder for someone new to C++ to read.
bool isalpha (char a)
{
return (((a)>='A' && (a) <= 'Z') ||((a)>='a' && (a) <= 'z'));
}
It takes a character a and checks whether the ASCII value of a is between the ASCII values of 'A' (65) and 'Z' (90) or between the ASCII values of 'a' (97) and 'z' (122). If it is, it's a letter. If not, it isn't. By "between", I am including the boundary conditions ('A', 'Z', 'a', 'z') as being "between". You don't have to write your own function. cctype provides the equivalent function: http://www.cplusplus.com/reference/clibrary/cctype/isalpha.html
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Original algorithm diagnosis: the patient's case is hopeless.
#include <ctype.h>
// Now we have isalpha()...
int HowEasy::pointVal(const string& s)
{
int slen = s.length();
int words = 0;
int letters = 0;
for (int i = 0; i < slen; ++i)
{
while (i < slen && !isalpha(s[i]))
++i;
if (i >= slen)
break;
++words;
++letters;
while (++i < slen && isalpha(s[i]))
++letters;
}
if (words == 0)
return 0;
int points;
int average = letters / words;
if (average <= 3)
points = 250;
else if (average < 6)
points = 500;
else
points = 1000;
return points;
}
Furthemore: try to avoid "in deep level printing" syndrome. The only goal of pointVal() member function is: calculate easy reading points. That's all. Let callee comments the result.
Pay attention on the right function parameter type. No need to copy argument string, pass argument by reference.
Clean up main function dialog. Why bla-bla-bla with "Press any key...", cin > t etc?
It's enough:
cout << "Type the problem statement:" << endl;;
getline(cin, class1.statement);
cout << "The problem statement's point value is: "
<< class1.pointVal(class1.statement)
<< endl;
Apropos, you have absolutely useless member in HowEasy... Right, it's string HowEasy::statement (public!). Do you think that there is extremely shortage of the memory outside HowEasy bounds?..
Let's remember now your post header: Linking error in the compiler...
A compiler does not perform linkage. It compiles...
Good luck!
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348