| | |
linked list
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
Hey,
I am having problems with this code, I think it is not linking up the whole file, because when I run it it only says "hi" 3 times. And also it keeps crashing because of best[i] = point->item->return_value();
here is the class it calls:
And here is the data in the file it reads:
14101 A 6
11100 C 2
20056 D 10
15594 D 8
23231 B 12
16649 A 5
22334 B 13
19876 C 10
22435 A 9
14231 B 8
16767 C 5
17890 D 11
19045 C 16
10001 A 4
20341 B 7
14578 C 23
20011 D 20
15560 B 10
I am having problems with this code, I think it is not linking up the whole file, because when I run it it only says "hi" 3 times. And also it keeps crashing because of best[i] = point->item->return_value();
C++ Syntax (Toggle Plain Text)
#include "applicant.cpp" #include <iostream> #include <fstream> #include <iomanip> using namespace std; struct Employeelink { int id; int yearsWorked; char skillLevel; Applicant *item; Employeelink *next; }; class Promosion { protected: float value; Employeelink *start; Employeelink *lastone; public: Promosion(void); void linkup(Employeelink); int best(void); }; Promosion::Promosion() { //start = NULL; lastone = NULL; } void Promosion::linkup(Employeelink employee) { Employeelink *another; another = new Employeelink; another->id = employee.id; another->skillLevel = employee.skillLevel; another->yearsWorked = employee.yearsWorked; lastone = another; //start = another; } int Promosion::best() { float value[18]; Employeelink *point; float best[18]; point = start; int i = 0; float totalBest; //point = point->next; while(point != NULL) { cout << "hi" << endl; best[i] = point->item->return_value(); point = point->next; i++; } for(int x = 0; x < i; x++) { for(int y = 0; y < i; y++) { if(best[x] > best[y]) totalBest = best[x]; } } } int main() { ifstream partin; Employeelink emp, temp; Promosion applicant; partin.open("applicnt.dat"); if(partin.fail()) { cout << "Error: Unable to open file!" << endl; partin.clear(); } partin >> temp.id; while(!partin.eof()) { emp.id = temp.id; partin >> emp.skillLevel; partin >> emp.yearsWorked; applicant.linkup(emp); } applicant.best(); system("pause"); return EXIT_SUCCESS; }
here is the class it calls:
C++ Syntax (Toggle Plain Text)
/*---------------------------------------------------------------- this file includes the class Applicant and a constant of 3.4 used to calculate an applicant's value filename: applicant.cpp ----------------------------------------------------------------*/ #include <iostream> using namespace std; const float indexnumber = 3.4; class Applicant { protected: int id; char skill; int years; float value; public: Applicant(void); void store_id(int); void store_skill(char); void store_years(int); void calc_value(void); int return_id(void); char return_skill(void); int return_years(void); float return_value(void); }; Applicant::Applicant() { id = 0; skill = '@'; years = 0; value = -1; } void Applicant::store_id(int who) { id = who; } void Applicant::store_skill(char level) { skill = level; } void Applicant::store_years(int howlong) { years = howlong; } void Applicant::calc_value() { float skillvalue, yearsvalue; switch(skill) { case 'D': skillvalue = indexnumber; break; case 'C': skillvalue = 1.3 * indexnumber; break; case 'B': skillvalue = 1.7 * indexnumber; break; case 'A': skillvalue = 2.1 * indexnumber; break; default: skillvalue = 0; } if (years <= 5) yearsvalue = years; else yearsvalue = 5 + (years - 5) * 0.5; value = skillvalue + yearsvalue; } int Applicant::return_id() { return id; } char Applicant::return_skill() { return skill; } int Applicant::return_years() { return years; } float Applicant::return_value() { if (value == -1) calc_value(); return value; }
14101 A 6
11100 C 2
20056 D 10
15594 D 8
23231 B 12
16649 A 5
22334 B 13
19876 C 10
22435 A 9
14231 B 8
16767 C 5
17890 D 11
19045 C 16
10001 A 4
20341 B 7
14578 C 23
20011 D 20
15560 B 10
>>#include "applicant.cpp"
Never, ever, under NO circumstances, do this!! DO NOT INCLUDE *.CPP like that. Instead, compile them separately and link the object modules. How to do that depends on the compiler you are using.
Never, ever, under NO circumstances, do this!! DO NOT INCLUDE *.CPP like that. Instead, compile them separately and link the object modules. How to do that depends on the compiler you are using.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
ok.
I changed the code a little, but at the couts I am getting weird numbers that are not in the file applicnt.dat as well as the numbers that are
here is the new code
I changed the code a little, but at the couts I am getting weird numbers that are not in the file applicnt.dat as well as the numbers that are
here is the new code
C++ Syntax (Toggle Plain Text)
#include "applicant.cpp" #include <iostream> #include <fstream> #include <iomanip> using namespace std; struct Employeelink { int id; int yearsWorked; char skillLevel; Applicant *item; Employeelink *next, *before; }; class Promosion { protected: float value; Employeelink *start; Employeelink *lastone; public: Promosion(void); void linkup(Employeelink); int best(void); }; Promosion::Promosion() { start = NULL; lastone = NULL; } void Promosion::linkup(Employeelink applicant) { Employeelink *another, *last, *here; another = new Employeelink; another->id = applicant.id; //cout << another->id << endl; another->skillLevel = applicant.skillLevel; //cout << another->skillLevel << endl; another->yearsWorked = applicant.yearsWorked; another->next = lastone; // lastone = another; if(start == NULL) { start = another; start->before = NULL; } else { here = start; } //start = another; } int Promosion::best() { float value[18]; float best[18]; //point = start; int i = 0; float totalBest; Employeelink *point = start; while(point != NULL) { //point->item->return_value(); //cout << "hi" << endl; best[i] = point->item->return_value(); //cout << "Hi" << i << endl; point = point->next; i++; } //cout << i; for(int x = 0; x < i; x++) { for(int y = 0; y < i; y++) { if(best[x] > best[y]) totalBest = best[x]; } } } int main() { int x = 0; ifstream partin; Employeelink emp, temp; Promosion applicant; Applicant *app; partin.open("applicnt.dat"); if(partin.fail()) { cout << "Error: Unable to open file!" << endl; partin.clear(); } partin >> temp.id; while(x < 18) { x++; //cout << x << endl; emp.id = temp.id; // partin >> emp->id; partin >> emp.skillLevel; //cout << emp->skillLevel << endl; partin >> emp.yearsWorked; cout << emp.yearsWorked << endl; //app->store_id(emp.id); //cout << app->return_id(); //app->store_years(emp.yearsWorked); //app->store_skill(emp.skillLevel); applicant.linkup(emp); } //cout << x; applicant.best(); system("pause"); return EXIT_SUCCESS; }
The while loop is incorrect
C++ Syntax (Toggle Plain Text)
while( partin >> emp.id >> emp.skillLevel >> emp.yearsWorked) { applicant.linkup(emp); }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
C++ Syntax (Toggle Plain Text)
point->item->return_value();
i saw you 'Promosion::linkup' function and nowhere you are assigning anything to the 'Applicant *item;'. it will be NULL or may be garbage when you access it.
2>system("pause"); is not a good way to do this, you can find a lot of threads here that tell you in detail why. use cin.get() instead.
2>divide applicant.cpp into applicant.h and applicant.cpp and include "applicant.h" and link applicant.cpp
Last edited by Agni; Dec 9th, 2008 at 12:26 am.
thanks
-chandra
-chandra
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Linked queue crashing
- Next Thread: Unable to find an entry point
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux 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 return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






