I'm having trouble working with c strings (not c++ class objects) and classes.
Here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <istream>
using namespace std;
int search( int srchVal);
class Employee
{
private:
int id; // employee ID
char name[21]; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
Employee( int initId=0, char initName[21]="",
double initHourlyPay=0.0,
int initNumDeps=0, int initType=0 ); // Constructor
bool set(int newId, char newName[21], double newHourlyPay,
int newNumDeps, int newType);
int getId ();
char getName();
double getPay();
int getNumDeps ();
int getType ();
};
Employee::Employee( int initId, char initName[21],
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName, initHourlyPay,
initNumDeps, initType );
if ( !status )
{
id = 0;
strcpy(name, "");
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, char newName[21], double newHourlyPay,
int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
strcpy(name, newName);
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
int Employee:: getId ()
{
return id;
}
char Employee:: getName()
{
return *name;
}
double Employee:: getPay()
{
return hourlyPay;
}
int Employee:: getNumDeps()
{
return numDeps;
}
int Employee:: getType()
{
return type;
}
int main ()
{
Employee emp[99];
int empId, empDep, empType;
double empPay;
char empName[21];
char empSex;
int index=0;
ifstream master9;
master9.open("master9.txt");
if (master9)
{
while (master9)
{
master9>>empId;
master9.ignore();
master9.getline(empName, 20, '\0');
master9.clear();
master9>>empPay;
master9>>empDep;
master9>>empType;
master9>>empSex;
emp[index].set(empId, empName, empPay, empDep, empType);
index++;
}
}
else
cout<<"Unable to open master file."<<endl;
master9.close();
for (int i=0; i<index; i++)
cout<<emp[i].getName();
system ("PAUSE");
return 0;
}
The problem is that it's only printing the first Character for each name in the input file. When I tested the variable empName, it works fine (the whole name is stored in THAT variable, I can't seem to get it to correctly update my object variables). It seems that the problem occurs in the set member function. I am lost. Any help will be appreciated. (I'm not looking for homework answers, just a little helpful insight. Believe me, the assignment is much longer than this problem ;) )
here is what the master9.txt input file looks like....
5 Christine Kim 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F
17 Juan Gonzales 30.00 1 1 M
18 Morris Kramer 8.95 0 0 M
22 Cindy Burke 15.00 1 0 F
24 Esther Bianco 10.25 0 0 F
25 Jim Moore 27.50 3 1 M
32 Paula Cameron 14.50 0 0 F
36 Melvin Ducan 10.25 0 0 M
37 Nina Kamran 30.00 1 1 F
38 Julie Brown 35.00 0 1 F
40 Imelda Buentello 14.50 0 0 F
42 J. P. Morgan 12.50 0 0 M
43 Maria Diaz 15.00 0 0 F
Long story short, since it didn't want to post my previous answer was that you're declaring the wrong return value for getName. The first returns just a character, the second returns a char pointer, also known as a C-String.
Also, please make sure that you're using the format tags for code and input/output of code. They can be found to the right of your replies, or underneath a new post. Use <> for code, and either TT or == input/output. Be sure to highlight your code before attempting to format it as well.