Hello all, I am at the beginning of a new program working with structs and I am getting an error I cannot figure out. My code so far is

//PJ901 PAtrick Nealey
//Acme Payroll Program

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring> // for _strcmp
using namespace std;

const int MaxNameLen = 21;			//sets maximm name lenght to 20
const int MaxEmployees = 500;			//sets maximum number of employee records to 500
enum PayType{Salary, Hourly};

struct Employee{
	int id;							//employee ID number
	char name[MaxNameLen];					//employee name
	double pay_rate;				//employee pay rate
	short int dependants;			//employee's number of dependants
	PayType pt;						//determines wether employee is paid hourly or is on salary
};


int LoadMaster (Employee employee[]);

int main()
{
	Employee employee[MaxEmployees];			//Array of employee recoeds
	int numemployees;					//number of employees in array
	numemployees = LoadMaster( employee );

//cout >>"number of employees =">>numemployees;

	
 }
int LoadMaster (Employee employee[MaxEmployees])
{
	 int idx = 0; //array index
	 char delimiter;  //delimiter symbol used in the input file.

	 ifstream infile( "PJ901master.txt" );

	 if( !infile )
	 {
		 cerr << "Could not open the input file." << endl;
		 exit(1);
	 }
	 
	 while( idx < MaxEmployees &&  infile >> delimiter && 
		 infile.getline( employee[ idx ].name, sizeof(employee[ idx ].name), ' ' ) )
	 {
		 infile >> employee[ idx].id >> employee[ idx].name 
			 >> employee[idx].pay_rate >> employee[idx].dependants >> employee[idx].pt;
		 idx++;
	 }
	 
	 //TEST THE INPUT STREAM STATUS
	 if( idx == MaxEmployees && infile >>ws && infile.good() )
	 {
         cerr << "Too many data.\n";
         exit( 3 );
     }
     if( !infile.eof() && infile.fail() )
     {
          cerr << "Bad data.\n";
          exit( 4 );
     }

	 return idx;
	 }

I am getting an error on line 52 which states "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'PayType' (or there is no acceptable conversion)"

Any ideas?

Recommended Answers

All 6 Replies

Break it into pieces and you'll quickly find the exact offending line:

infile >> employee[ idx].id;
		 infile >> employee[ idx].name;
		 infile >> employee[idx].pay_rate;
		 infile >> employee[idx].dependants;
		 infile >> employee[idx].pt;  // this line

You'll need to write a >> operator for your PayType enum. What is the appropriate input from the text file? Integers? Strings of "Hourly" or "Salary"?

Or if you don't want to write a >> operator, read it in as a string or int and do an if-else statement as an assignment.

In the actual input file there is simply characters (either S or H)...I havent worked with operators yet, so I am unclear how to do that....any suggestions?

If you don't know how to do operator overloads yet, you'll want a char-type variable that you read into. After you read in the char, use a switch or an if/else to set the actual value of your employee::pt member.

Then the thing to do is have a local char variable that you read the file into and then use a switch/if statement to decode the value into the actual value for employee[idx].pt

do i do this in the same function as i read it in?

May as well. All you have to do is add a half-dozen lines or so to the function. Why take more steps than absolutely necessary?

Be a part of the DaniWeb community

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