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 = point->item->return_value();


#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:

/*----------------------------------------------------------------
   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;
}

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

Recommended Answers

>>#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.

Jump to Post

The while loop is incorrect

while( partin >> emp.id >> emp.skillLevel >> emp.yearsWorked)
{
    applicant.linkup(emp);
}
Jump to Post

All 5 Replies

>>#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.

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

#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

while( partin >> emp.id >> emp.skillLevel >> emp.yearsWorked)
{
    applicant.linkup(emp);
}

that makes sense! Its always a simple solution lol.
Thank you very much!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.