| | |
Linking error in the compiler given by topcoder. Plz help me out guys.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,814
Reputation:
Solved Threads: 501
•
•
•
•
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,
C++ Syntax (Toggle Plain Text)
#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.
C++ Syntax (Toggle Plain Text)
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/c...e/isalpha.html
Original algorithm diagnosis: the patient's case is hopeless.
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:
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!
C++ Syntax (Toggle Plain Text)
#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; }
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:
C++ Syntax (Toggle Plain Text)
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!
Last edited by ArkM; Aug 6th, 2008 at 4:09 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Circular List help
- Next Thread: winsock help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






