| | |
Linking error in the compiler given by topcoder. Plz help me out guys.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
#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); }
•
•
•
•
What compiler are you using?
I can't see any error messages in your post.
Whyint pointVal (string s)and where isint HowEasy::pointVal(string s)MEMBER FUNCTION definition? Probably, it's a reason of your troubles...
int HowEasy::pointVal(string s) also. It shows a different set of errors for that. 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++.
PS: Im just somewhere between beginner and intermediate. If i post some thing wrong, plz correct me and help me learn more about C++.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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. Well, I have made some assumptions since you did not say what it is supposed to do.
I modified your code a little
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
I modified your code a little

cpp Syntax (Toggle Plain Text)
#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() ; }
•
•
•
•
Originally Posted by Output
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
Last edited by Prabakar; Aug 6th, 2008 at 2:06 pm. Reason: Format the code
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
PS: I've posted the error message i get from the compiler below.
#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.
C++ Syntax (Toggle Plain Text)
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
Last edited by arun_lisieux; Aug 6th, 2008 at 2:50 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Circular List help
- Next Thread: winsock help
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






