943,735 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1336
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 6th, 2008
0

Linking error in the compiler given by topcoder. Plz help me out guys.

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5. class HowEasy
  6. {
  7. public:
  8. string statement;
  9. int flag,letterCount,wordCount;
  10. int pointVal (string);
  11. };
  12.  
  13. int pointVal (string s)
  14. {
  15. int len = s.length();
  16. int avgCount,flag=0,i,wordCount=0,letterCountMain=0,letterCountTemp=0;
  17. for(i=0; i<len ;i++)
  18. {
  19. //if((int(s[i]>=65) && int(s[i]<=90)) || int((s[i]>=97) && int(s[i]<=122)))
  20. if(((s[i]>=65)&&(s[i]<=90))||((s[i]>=97)&&(s[i]<=122)))
  21. {
  22. if(flag==0)
  23. {
  24. letterCountTemp+=1;
  25. }
  26. }
  27. else
  28. {
  29. flag=1;
  30. letterCountTemp=0;
  31. }
  32. if(s[i]==' ')
  33. {
  34. if(flag==0)
  35. {
  36. letterCountMain+=letterCountTemp;
  37. wordCount+=1;
  38. }
  39. flag=0;
  40. }
  41. if(s[i]=='.')
  42. {
  43. if(flag==0 && s[i+1]!='.')
  44. {
  45. letterCountMain+=letterCountTemp;
  46. wordCount+=1;
  47. }
  48. flag=0;
  49. }
  50. }
  51. cout << "The no of letters in the problem statement is displayed below: " << letterCountMain << endl;
  52. cout << "The no of words in the problem statement is displayed below: " << wordCount << endl;
  53. avgCount = letterCountMain/wordCount;
  54. cout << "The avg letter count in the problem statement is displayed below: " << avgCount << endl << endl;
  55. if(avgCount<=3)
  56. {
  57. return 250;
  58. }
  59. else if((avgCount==4)||(avgCount==5))
  60. {
  61. return 500;
  62. }
  63. else if(avgCount>6)
  64. {
  65. return 1000;
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71. HowEasy class1;
  72. char t;
  73. cout << "Press any key to type the problem statement: ";
  74. cin >> t;
  75. cin.get();
  76. cout << "Type the problem statement: ";
  77. getline(cin, class1.statement);
  78. cout << "The problem statement's point value is :" << class1.pointVal(class1.statement);
  79. }
Reputation Points: 13
Solved Threads: 0
Light Poster
arun_lisieux is offline Offline
38 posts
since Aug 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

Click to Expand / Collapse  Quote originally posted by ArkM ...
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.
Reputation Points: 13
Solved Threads: 0
Light Poster
arun_lisieux is offline Offline
38 posts
since Aug 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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?..
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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++.
Reputation Points: 13
Solved Threads: 0
Light Poster
arun_lisieux is offline Offline
38 posts
since Aug 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

Thanks for the suggestion man. Will try it now.
Reputation Points: 13
Solved Threads: 0
Light Poster
arun_lisieux is offline Offline
38 posts
since Aug 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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

cpp Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #define isalpha(a) (((a)>='A' && (a) <= 'Z') ||((a)>='a' && (a) <= 'z'))
  4.  
  5. using namespace std;
  6.  
  7. class HowEasy
  8. {
  9. public:
  10. int pointVal (string);
  11. string statement;
  12. private:
  13. int letterCount,wordCount;
  14.  
  15. };
  16.  
  17. int HowEasy::pointVal (string s)
  18. {
  19. int len = s.length();
  20. int avgCount, i ;
  21. wordCount=0,letterCount=0 ;
  22. for(i=0; i<len ;i++)
  23. {
  24. if(isalpha(s[i]))
  25. letterCount++;
  26. else if(s[i]==' ')
  27. wordCount++;
  28. else
  29. {
  30. while ( !isalpha(s[i+1])) i++ ;
  31. wordCount++;
  32. }
  33. }
  34. cout << "The no of letters in the string: " << letterCount << endl;
  35. cout << "The no of words in the string: " << wordCount << endl;
  36. avgCount = letterCount/wordCount;
  37. cout << "The avg letter count in the string: " << avgCount << endl ;
  38. if(avgCount<=3)
  39. return 250;
  40. else if(avgCount<=5)
  41. return 500;
  42. else
  43. return 1000;
  44. }
  45.  
  46. int main()
  47. {
  48. HowEasy class1;
  49. cout << "Type the problem statement: ";
  50. getline(cin, class1.statement);
  51. cout << "The string's point value is :" << class1.pointVal(class1.statement);
  52. cin.get() ;
  53. }

Quote 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
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
Last edited by Prabakar; Aug 6th, 2008 at 2:06 pm. Reason: Format the code
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

Oops, Some syntax errors. I changed it now.
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008
Aug 6th, 2008
0

Re: Linking error in the compiler given by topcoder. Plz help me out guys.

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.

C++ Syntax (Toggle Plain Text)
  1. your code did not compile:
  2.  
  3. errors linking:
  4.  
  5. HowEasy-stub.o.(.text+0x534) : In function 'main':
  6. : multiple definition of 'main'
  7. HowEasy.o.(.text+0x3fc): first defined here
  8. /usr/bin/ld: warning: size of symbol 'main' changed from 269 in HowEasy.o to 620 in HowEasy-stub.o
  9. collect2: ld returned 1 exit status
Last edited by arun_lisieux; Aug 6th, 2008 at 2:50 pm.
Reputation Points: 13
Solved Threads: 0
Light Poster
arun_lisieux is offline Offline
38 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Circular List help
Next Thread in C++ Forum Timeline: winsock help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC