Guys,
Im not very experienced in C++. I was practicing problems from topcoder to improve my knowledge in C++. I've posted the code i used for solving a problem statement given by topcoder website. The compiler tells that there is a linking error with this code and hence its not compiling properly. Plz help me out.

#include<iostream.h>
#include<string.h>
using namespace std;

class HowEasy
{
	public:
		string statement;
                int flag,letterCount,wordCount;
                int pointVal (string);
};

int pointVal (string s)
{
    int len = s.length();
    int avgCount,flag=0,i,wordCount=0,letterCountMain=0,letterCountTemp=0;
    for(i=0; i<len ;i++)
    {
        //if((int(s[i]>=65) && int(s[i]<=90)) || int((s[i]>=97) && int(s[i]<=122)))
        if(((s[i]>=65)&&(s[i]<=90))||((s[i]>=97)&&(s[i]<=122)))
        {
            if(flag==0)
            {
                letterCountTemp+=1;
            }
        }
        else
        {
            flag=1;
            letterCountTemp=0;
        }
        if(s[i]==' ')
        {
            if(flag==0)
            {
                letterCountMain+=letterCountTemp;
                wordCount+=1;
            }
            flag=0;
        }
        if(s[i]=='.')
        {
            if(flag==0 && s[i+1]!='.')
            {
                letterCountMain+=letterCountTemp;
                wordCount+=1;
            }
            flag=0;
        }
    }
    cout << "The no of letters in the problem statement is displayed below: " << letterCountMain << endl;
    cout << "The no of words in the problem statement is displayed below: " << wordCount << endl;
    avgCount = letterCountMain/wordCount;
    cout << "The avg letter count in the problem statement is displayed below: " << avgCount << endl << endl;
    if(avgCount<=3)
    {
        return 250;
    }
    else if((avgCount==4)||(avgCount==5))
    {
        return 500;
    }
    else if(avgCount>6)
    {
        return 1000;
    }
}

int main()
{
    HowEasy class1;	
    char t;
    cout << "Press any key to type the problem statement: ";
    cin >> t;
    cin.get();
    cout << "Type the problem statement: ";
    getline(cin, class1.statement);
    cout << "The problem statement's point value is :" << class1.pointVal(class1.statement);
}

Recommended Answers

All 13 Replies

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

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

I didn't get any errors in Borland C++ compiler. I was just getting wrong outputs. But when i used this code on the compiler given in topcoder application, it gives me linking error. And i've tried int HowEasy::pointVal(string s) also. It shows a different set of errors for that.

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?..

My code had some logical errors in it. I got some output, but its not the expected output for the problem. This was when i used Borland C++. When i pasted the same code in the topcoder's compiler, im getting linking error. Any ideas on this?

PS: Im just somewhere between beginner and intermediate. If i post some thing wrong, plz correct me and help me learn more about C++.

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.

Thanks for the suggestion man. Will try it now.

Well, I have made some assumptions since you did not say what it is supposed to do.
I modified your code a little:)

#include<iostream>
#include<string>
#define isalpha(a) (((a)>='A' && (a) <= 'Z') ||((a)>='a' && (a) <= 'z'))

using namespace std;

class HowEasy
{
    public:
      int pointVal (string);
      string statement;
    private:
       int letterCount,wordCount;

};

int HowEasy::pointVal (string s)
{
    int len = s.length();
    int avgCount, i ;
    wordCount=0,letterCount=0 ;
    for(i=0; i<len ;i++)
    {
        if(isalpha(s[i]))
            letterCount++;
        else if(s[i]==' ')
            wordCount++;
        else
        { 
            while ( !isalpha(s[i+1])) i++ ;
            wordCount++;
        }
    }
    cout << "The no of letters in the string: " << letterCount << endl;
    cout << "The no of words in the string: " << wordCount << endl;
    avgCount = letterCount/wordCount;
    cout << "The avg letter count in the string: " << avgCount << endl ;
    if(avgCount<=3)
        return 250;
    else if(avgCount<=5)
        return 500;
    else 
        return 1000;
}

int main()
{
    HowEasy class1;	
    cout << "Type the problem statement: ";
    getline(cin, class1.statement);
    cout << "The string's point value is :" << class1.pointVal(class1.statement);
    cin.get() ;
}

Type the problem statement: Hi! This Statement should be fine. I hope so...
The no of letters in the problem statement is displayed below: 34
The no of words in the problem statement is displayed below: 9
The avg letter count in the problem statement is displayed below: 3

The problem statement's point value is :250

EDIT: I don't like using string data as a public member. I didn't care about such things here since I was interested in fixing the errors

Oops, Some syntax errors. I changed it now.

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). Your code works. But it considers words with numbers and two full stops "..", but they must be omitted from the count of words. Thats why i used the flag variable. May i know wat went wrong in that approach? 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?

PS: I've posted the error message i get from the compiler below.

your code did not compile:

errors linking:

HowEasy-stub.o.(.text+0x534) : In function 'main':
: multiple definition of 'main'
HowEasy.o.(.text+0x3fc): first defined here
/usr/bin/ld: warning: size of symbol 'main' changed from 269 in HowEasy.o to 620 in HowEasy-stub.o
collect2: ld returned 1 exit status

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

Thanks VernonDozier. That explains things. And do you have any clue about why im getting the error which i've edited in my previous post?

Perhaps you may have to toggle to plain text before coping the code. Here is the code again.

EDIT: and I saw the explanation. So flag is indeed need incase the word contains text & numbers. Well, add that in your code should be simple:)

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.