I'm working on a code for a school project which I can't use strings.
I'm having problems getting the value for hourlyPay.
The program's output:
5 Christine Kim 4.94066e-324
Although, the file contains the following:
5 Christine Kim 30.00
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
//Class decleration
class Employee
{
private:
int id; //Employee ID.
char name[21]; //Employee name.
double hourlyPay; //Pay per hour.
public:
Employee(int initId=0, char [] =0, double initHourlyPay=0.0); //Constructor.
bool set(int newId, char [], double newHourlyPay);
int getId() { return id; }
const char * getName() { return name;}
double getHourlyPay() { return hourlyPay;}
};
Employee::Employee( int initId, char initName[], double initHourlyPay)
{
bool status = set( initId, initName, initHourlyPay);
if ( !status )
{
id = 0;
strcpy(name, "");
hourlyPay = 0.0;
}
}
bool Employee::set( int newId, char newName[], double newHourlyPay)
{
bool status = false;
if ( newId > 0)
{
status = true;
id = newId;
strcpy(name, newName);
hourlyPay = newHourlyPay;
}
return status;
}
const int MAX_SIZE = 100;
int main()
{
int id; //Employee ID.
char newName[21];
double hourlyPay; //Pay per hour.
Employee list[15]; //Array to store
ifstream masterFile; //Opens master file.
masterFile.open("master10.txt");
int count = 0;
if (masterFile)
{
for (count; count < 2; count++)
{
masterFile >> id;
masterFile.ignore();
masterFile.getline(newName, 21);
masterFile >> hourlyPay;
list[count].set(id, newName, hourlyPay);
}
}
masterFile.close(); //Close master file.
cout << list[0].getId() << " " << list[0].getName() << " " << list[0].getHourlyPay();
}
The original file contains more lines, but I narrowed it down in order to figure out my error.
What am I doing wrong?
I have figured it out.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
//Class decleration
class Employee
{
private:
int id; //Employee ID.
char name[21]; //Employee name.
double hourlyPay; //Pay per hour.
public:
Employee(int initId=0, char [] =0, double initHourlyPay=0.0); //Constructor.
bool set(int newId, char [], double newHourlyPay);
int getId() { return id; }
const char * getName() { return name;}
double getHourlyPay() { return hourlyPay;}
};
Employee::Employee( int initId, char initName[], double initHourlyPay)
{
bool status = set( initId, initName, initHourlyPay);
if ( !status )
{
id = 0;
strcpy(name, "");
hourlyPay = 0.0;
}
}
bool Employee::set( int newId, char newName[], double newHourlyPay)
{
bool status = false;
if ( newId > 0)
{
status = true;
id = newId;
strcpy(name, newName);
hourlyPay = newHourlyPay;
}
return status;
}
const int MAX_SIZE = 100;
int main()
{
int id; //Employee ID.
char newName[21];
double hourlyPay; //Pay per hour.
Employee list[15]; //Array to store
ifstream masterFile; //Opens master file.
masterFile.open("master10.txt");
int count = 0;
if (masterFile)
{
for (count; count < 2; count++)
{
masterFile >> id;
masterFile.ignore();
masterFile.get(newName, 21);
masterFile >> hourlyPay;
list[count].set(id, newName, hourlyPay);
}
}
masterFile.close(); //Close master file.
cout << list[0].getId() << " " << list[0].getName() << " " << list[0].getHourlyPay();
}
I only changed getline to get and now it can read in the middle of a line with a limit of 20 chars. I appreciate everyone's attention and help.