Ok, my problem is that I'm reading from a file 3 values, a last name, first name and salary, for example: Key Bobby 43000
I have an array of objects for each person, so I neeed to read this file and assign the last name, first name, and salary to the appropriate variable. I've been out of C++ since April. I'm trying to do this for my Dad's business and I'm rusty. I have some debug statements in there to see if I'm actually inputing values. As of now I do not know what to pass to the set functions in my while loop because I don't want it to be to ineffecient. Any help is appreciated, I may be totally wrong with what I'm doing, let me know.
My header so far.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Employee
{
string first_name, // Employee first name
last_name; // Employee last name
float salary; // Monthly salary amount received
public:
// Employee's constructors
Employee();
// Set member variables
void set_first_name(string employee_first_name);
void set_last_name (string employee_first_name);
void set_salary (float monthly_salary);
// Get member variables
string get_first_name() {return first_name;}
string get_last_name () {return last_name;}
float get_salary () {return salary;}
void print_employee_info();
};
Employee::Employee()
{
first_name = '\0';
last_name = '\0';
salary = 0.0f;
}
void Employee::set_first_name(string employee_first_name)
{
first_name = employee_first_name;
}
void Employee::set_last_name(string employee_last_name)
{
last_name = employee_last_name;
}
void Employee::set_salary(float monthly_salary)
{
if(monthly_salary > 0.0f)
salary = monthly_salary;
else
salary = 0.0f;
}
My main.
#include "stdafx.h"
#include "EmployeeHeader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define MAX_EMPLOYEE_SIZE 100
int main()
{
ifstream indata;
Employee EmployeeInfo[MAX_EMPLOYEE_SIZE];
string name;
int employee_count = 0;
indata.open("employee.txt");
if(!indata)
{
cout << "Error: file could not be opened" << endl;
exit(1);
}
while ( !indata.eof() )
{
indata >> EmployeeInfo[employee_count].set_first_name();
cout << EmployeeInfo[employee_count].get_first_name(); // debug
indata >> EmployeeInfo[employee_count].set_last_name();
cout << EmployeeInfo[employee_count].get_last_name(); // debug
indata >> EmployeeInfo[employee_count].set_salary();
cout << EmployeeInfo[employee_count].get_salary(); // debug
employee_count++;
}
indata.close();
cout << "End-of-file reached." << endl;
return 0;
}