I have this question and I am really not sure what to do.
the question has 2 parts I solved the first part which wants me to do the following :
Write a program that creates an output file named “employee.txt”. The program then asks the user to enter employee name, employee ID number, number of working hours in one month, paying rate per hour and stores them in the file in the following sequence: employee name (string), employee ID number (integer), number of working hours in one month (integer) and paying rate per hour (double). The program stores each employee data in a single line in the file.
Sample output file:
Jack 123 25 35.5
Sara 122 31 31.2
Sam 145 28 22.9
but the second part wants me to read the txt file from the first part and compute the salary for each employee.
I don't know how to read the txt file as it has strings and spaces ._.
this is what I tried
It gives me this error :
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
int ID; //employee ID number
int hours;//employee number of working hours in a month
double pay; //paying rate per hour
double salary;
usingnamespace std;
int main()
{
string name; //employee name
ifstream oldtxt;
oldtxt.open ("employee.txt", ios::in);
ofstream newtxt;
newtxt.open ("Salary.txt");
if(!oldtxt)
{
cerr << "File could not be opened, Make sure you put it in the same directory as this .CPP file!" << endl;
exit( 1 );
}
for (int h = 0 ; h < 3 ; h++)
{
//trying to read the string and numbers from the old txt file and at the same time writing them to the new txt file
//It gives me error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
oldtxt >> name;
oldtxt >> setw(2);
newtxt << name;
newtxt << setw(2);
oldtxt >> ID;
oldtxt >> setw(2);
newtxt << ID;
newtxt << setw(2);
oldtxt >> hours;
oldtxt >> setw(2);
oldtxt >> pay;
salary = hours * pay;
oldtxt >> endl;
newtxt << salary;
newtxt << endl;
}
oldtxt.close();
newtxt.close();
return 0 ;
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
struct inv_empdata{ // I used a structure to store the info in, this will make it easier later to
char emp_name[4]; // store and alter the data in memory if it is needed.
int emp_id;
int emp_hours;
double emp_pay;
};
int main()
{
inv_empdata empdata;
ifstream txtdata_in;
txtdata_in.open ("employee_in.txt");
ofstream txtdata_out;
txtdata_out.open("employee_out.txt");
if(!txtdata_in)
{
cerr << "File could not be opened, Make sure you put it in the same directory as this .CPP file!" << endl;
return 1;
}
if(!txtdata_out)
{
cerr << "File could not be opened, Make sure you put it in the same directory as this .CPP file!" << endl;
return 1;
}
for (int counter = 0; counter < 3; counter++) {
txtdata_in >> empdata.emp_name;
txtdata_out << empdata.emp_name << " ";
txtdata_in >> empdata.emp_id;
txtdata_out << empdata.emp_id << " ";
//
txtdata_in >> empdata.emp_hours;
txtdata_out << empdata.emp_hours << " ";
//
txtdata_in >> empdata.emp_pay;
txtdata_out << empdata.emp_pay << " ";
txtdata_out << (empdata.emp_hours * empdata.emp_pay) << endl;
}
txtdata_in.close();
txtdata_out.close();
cout << "Finished" << endl;
return 0 ;
This is what I came up with, it's a quick hack to show how to write and read from the file I/O. I will also format the output to look like the input with salary added.
tfityo
That solved the problem ! :) I thought that spaces and setw() are very important when reading from a .txt file.
vrnichol
Thank you for your hard work .. too bad our professor didn't teach us structures yet.
But thanks for the help you showed me another way to solve the same problem :)!