943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 551
  • C++ RSS
Dec 8th, 2008
0

linked list

Expand Post »
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();

C++ Syntax (Toggle Plain Text)
  1. #include "applicant.cpp"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct Employeelink
  9. {
  10. int id;
  11. int yearsWorked;
  12. char skillLevel;
  13. Applicant *item;
  14. Employeelink *next;
  15.  
  16. };
  17.  
  18. class Promosion
  19. {
  20. protected:
  21. float value;
  22. Employeelink *start;
  23. Employeelink *lastone;
  24. public:
  25. Promosion(void);
  26. void linkup(Employeelink);
  27. int best(void);
  28. };
  29.  
  30. Promosion::Promosion()
  31. {
  32. //start = NULL;
  33. lastone = NULL;
  34. }
  35.  
  36. void
  37. Promosion::linkup(Employeelink employee)
  38. {
  39. Employeelink *another;
  40. another = new Employeelink;
  41. another->id = employee.id;
  42. another->skillLevel = employee.skillLevel;
  43. another->yearsWorked = employee.yearsWorked;
  44.  
  45. lastone = another;
  46. //start = another;
  47. }
  48.  
  49. int
  50. Promosion::best()
  51. {
  52. float value[18];
  53. Employeelink *point;
  54. float best[18];
  55. point = start;
  56. int i = 0;
  57. float totalBest;
  58. //point = point->next;
  59. while(point != NULL)
  60. {
  61. cout << "hi" << endl;
  62. best[i] = point->item->return_value();
  63. point = point->next;
  64. i++;
  65. }
  66. for(int x = 0; x < i; x++)
  67. {
  68. for(int y = 0; y < i; y++)
  69. {
  70. if(best[x] > best[y])
  71. totalBest = best[x];
  72.  
  73. }
  74. }
  75. }
  76.  
  77. int
  78. main()
  79. {
  80. ifstream partin;
  81. Employeelink emp, temp;
  82. Promosion applicant;
  83.  
  84. partin.open("applicnt.dat");
  85. if(partin.fail())
  86. {
  87. cout << "Error: Unable to open file!" << endl;
  88. partin.clear();
  89. }
  90.  
  91. partin >> temp.id;
  92. while(!partin.eof())
  93. {
  94. emp.id = temp.id;
  95. partin >> emp.skillLevel;
  96. partin >> emp.yearsWorked;
  97. applicant.linkup(emp);
  98. }
  99.  
  100. applicant.best();
  101.  
  102. system("pause");
  103. return EXIT_SUCCESS;
  104. }

here is the class it calls:
C++ Syntax (Toggle Plain Text)
  1.  
  2. /*----------------------------------------------------------------
  3.   this file includes the class Applicant and a constant of 3.4
  4.   used to calculate an applicant's value
  5.   filename: applicant.cpp
  6. ----------------------------------------------------------------*/
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. const float indexnumber = 3.4;
  13.  
  14. class Applicant
  15. {
  16. protected:
  17. int id;
  18. char skill;
  19. int years;
  20. float value;
  21. public:
  22. Applicant(void);
  23. void store_id(int);
  24. void store_skill(char);
  25. void store_years(int);
  26. void calc_value(void);
  27. int return_id(void);
  28. char return_skill(void);
  29. int return_years(void);
  30. float return_value(void);
  31. };
  32.  
  33. Applicant::Applicant()
  34. {
  35. id = 0;
  36. skill = '@';
  37. years = 0;
  38. value = -1;
  39. }
  40.  
  41. void
  42. Applicant::store_id(int who)
  43. {
  44. id = who;
  45. }
  46.  
  47. void
  48. Applicant::store_skill(char level)
  49. {
  50. skill = level;
  51. }
  52.  
  53. void
  54. Applicant::store_years(int howlong)
  55. {
  56. years = howlong;
  57. }
  58.  
  59. void
  60. Applicant::calc_value()
  61. {
  62. float skillvalue, yearsvalue;
  63.  
  64. switch(skill)
  65. {
  66. case 'D': skillvalue = indexnumber;
  67. break;
  68. case 'C': skillvalue = 1.3 * indexnumber;
  69. break;
  70. case 'B': skillvalue = 1.7 * indexnumber;
  71. break;
  72. case 'A': skillvalue = 2.1 * indexnumber;
  73. break;
  74. default: skillvalue = 0;
  75. }
  76.  
  77. if (years <= 5)
  78. yearsvalue = years;
  79. else
  80. yearsvalue = 5 + (years - 5) * 0.5;
  81.  
  82. value = skillvalue + yearsvalue;
  83. }
  84.  
  85. int
  86. Applicant::return_id()
  87. {
  88. return id;
  89. }
  90.  
  91. char
  92. Applicant::return_skill()
  93. {
  94. return skill;
  95. }
  96.  
  97. int
  98. Applicant::return_years()
  99. {
  100. return years;
  101. }
  102.  
  103. float
  104. Applicant::return_value()
  105. {
  106. if (value == -1)
  107. calc_value();
  108. return value;
  109. }
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
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Dec 8th, 2008
0

Re: linked list

>>#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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 8th, 2008
0

Re: linked list

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
C++ Syntax (Toggle Plain Text)
  1. #include "applicant.cpp"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct Employeelink
  9. {
  10. int id;
  11. int yearsWorked;
  12. char skillLevel;
  13. Applicant *item;
  14. Employeelink *next, *before;
  15.  
  16. };
  17.  
  18. class Promosion
  19. {
  20. protected:
  21. float value;
  22. Employeelink *start;
  23. Employeelink *lastone;
  24. public:
  25. Promosion(void);
  26. void linkup(Employeelink);
  27. int best(void);
  28. };
  29.  
  30. Promosion::Promosion()
  31. {
  32. start = NULL;
  33. lastone = NULL;
  34. }
  35.  
  36. void
  37. Promosion::linkup(Employeelink applicant)
  38. {
  39. Employeelink *another, *last, *here;
  40. another = new Employeelink;
  41.  
  42. another->id = applicant.id;
  43. //cout << another->id << endl;
  44. another->skillLevel = applicant.skillLevel;
  45. //cout << another->skillLevel << endl;
  46. another->yearsWorked = applicant.yearsWorked;
  47. another->next = lastone;
  48.  
  49. // lastone = another;
  50. if(start == NULL)
  51. {
  52. start = another;
  53. start->before = NULL;
  54. }
  55. else
  56. {
  57. here = start;
  58. }
  59. //start = another;
  60. }
  61.  
  62. int
  63. Promosion::best()
  64. {
  65. float value[18];
  66. float best[18];
  67. //point = start;
  68. int i = 0;
  69. float totalBest;
  70.  
  71. Employeelink *point = start;
  72. while(point != NULL)
  73. {
  74. //point->item->return_value();
  75. //cout << "hi" << endl;
  76. best[i] = point->item->return_value();
  77. //cout << "Hi" << i << endl;
  78. point = point->next;
  79. i++;
  80. }
  81. //cout << i;
  82. for(int x = 0; x < i; x++)
  83. {
  84. for(int y = 0; y < i; y++)
  85. {
  86. if(best[x] > best[y])
  87. totalBest = best[x];
  88.  
  89. }
  90. }
  91. }
  92.  
  93. int
  94. main()
  95. {
  96. int x = 0;
  97. ifstream partin;
  98. Employeelink emp, temp;
  99. Promosion applicant;
  100. Applicant *app;
  101.  
  102. partin.open("applicnt.dat");
  103. if(partin.fail())
  104. {
  105. cout << "Error: Unable to open file!" << endl;
  106. partin.clear();
  107. }
  108.  
  109. partin >> temp.id;
  110. while(x < 18)
  111. {
  112. x++;
  113. //cout << x << endl;
  114. emp.id = temp.id;
  115. // partin >> emp->id;
  116. partin >> emp.skillLevel;
  117. //cout << emp->skillLevel << endl;
  118. partin >> emp.yearsWorked;
  119. cout << emp.yearsWorked << endl;
  120. //app->store_id(emp.id);
  121. //cout << app->return_id();
  122. //app->store_years(emp.yearsWorked);
  123. //app->store_skill(emp.skillLevel);
  124. applicant.linkup(emp);
  125. }
  126. //cout << x;
  127.  
  128. applicant.best();
  129.  
  130. system("pause");
  131. return EXIT_SUCCESS;
  132. }
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Dec 8th, 2008
0

Re: linked list

The while loop is incorrect
C++ Syntax (Toggle Plain Text)
  1. while( partin >> emp.id >> emp.skillLevel >> emp.yearsWorked)
  2. {
  3. applicant.linkup(emp);
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 9th, 2008
0

Re: linked list

that makes sense! Its always a simple solution lol.
Thank you very much!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Dec 9th, 2008
1

Re: linked list

C++ Syntax (Toggle Plain Text)
  1. point->item->return_value();
should give a segementation fault.

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Linked queue crashing
Next Thread in C++ Forum Timeline: Unable to find an entry point





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


Follow us on Twitter


© 2011 DaniWeb® LLC