I am working on a small program for the school. I have done everything, but the output is not as expected to be.
the program is reading data from a text file and using them for some calculation and storing the result in a new text file.
#include <iostream> //input output stream
#include <fstream> //file stream
#include <iomanip> //input output manipulation
usingnamespace std;
//Function Prototype
bool process_employee (ifstream& inFile);
void process_payroll (ifstream& inFile);
void print_results(ofstream& outFile);
int main()
{
double gross = 0;
double bonus = 0;
double adjGross = 0;
//Declare file to be read from
ifstream inFile;
inFile.open("empInFile.txt");//open the file
if(!inFile)
{
cout << "\n\nThe input file has not opened.\n"
<< "Check that the file exists.\n" << endl;
system("pause");
exit(1);
}//IF statement to test that the inFile opens
//Calls to the process_payroll funtion to perform calculations
process_payroll(inFile);
system("pause");
return 0;
}//main
//This function reads the employee information from the input file and returns them through referenced variables
//if the employee information is read successfully it returns 1 otherwise it returns 0.
bool process_employee (ifstream& inFile)
{
//Declare arrays
char employee_name [15];
double hourly_wage [15];
int hours_worked [15];
if(inFile >> employee_name, hourly_wage, hours_worked)
return 1;
elsereturn 0;
}// process_employee function
void process_payroll (ifstream& inFile)
{
//Local Declarations
ofstream outFile;
//Open the outFile
outFile.open("empReport.txt");//open the file
if (!outFile)
{
cout << "\n\nThe output file has not opened.\n"
<< endl;
system("pause");
exit(1);
}//IF statement to test that the outFile opens
else
{
cout << "Every thing is working fine." << endl;
}
//Calling process_employee function from inside a loop until zero is returned.
while (process_employee(inFile))
{
//Calls to the print_summary function to output all calculations
print_results(outFile);
}
//*******************************************************************
//close the ifstream inFile connection between the program and the file input.txt
inFile.close();
return;
}
//This function prints out the Summary message;
void print_results(ofstream& outFile)
{
//Local Declarations
int hours = 0;
int rate = 0;
int minWage = 0;
int maxWage = 0;
double minValue = 100;
double maxValue = 0;
double totAvg = 0;
//Declare arrays
char employee_name [15];
double hourly_wage [15];
int hours_worked [15];
int empFiles = employee_name[15];
//Statements
for(int i = 0; i < empFiles; i++)
{
if(hourly_wage[i] > maxValue)
{
maxValue = hourly_wage[i];
maxWage = i;
}
if(minValue > hourly_wage[i])
{
minValue = hourly_wage[i];
minWage = i;
}
totAvg = totAvg + hourly_wage[i];
}//For Loop
//The output of the minimum and maximum wage calculations and the average wage
outFile << "\n\nThe number of records read: " << empFiles << "\n"
<< "Maximum Pay Rate: " << employee_name[maxWage] << " @ $" << hourly_wage[maxWage] << "\n"
<< "Minimum Pay Rate: " << employee_name[minWage] << " @ $" << hourly_wage[minWage] << "\n"
<< "Average Pay: $" << totAvg / empFiles << endl;
if(empFiles >= 10)
{
outFile << "More than 10 names have been read in."<<endl;//error processing in case more than 10 names are attempted to be read in.
}
outFile << "\n\nOctober 2009 Payroll:\t\t\n\n";
outFile << "Employee Name \t Hours \t Rate \t Gross Bonus \t Adjusted_Gross" << endl;
outFile << "\n\n\n";
//format the ouput data
outFile << std::fixed << setprecision(2) ;
//Calculate the max and min wages
//Begin a series of true or false tests with IF Statements and specific expressions for each scenario
if(hours_worked[hours] > 45)//IF Statement for hours greater than 45
{
//Local declaration
double gross = (hourly_wage[rate] * hours_worked[hours]);
char bonus = 'Y';
double adjGross = gross + 50;
//Statements
outFile << "\n "<< employee_name << "\t" << hours_worked[hours] << "\t" << hourly_wage[rate] << "\t" << gross << "\t" << bonus << "\t" << adjGross << endl;
}//IF Statement for hours greater than 45
if(hours < 30)//IF Statement for hours less than 30
{
//Local Declarations and Statements of expressions for calculating each variable
double gross = (hourly_wage[rate] * hours_worked[hours]);
char bonus = 'N';
double adjGross = gross - (hourly_wage[rate] * hours_worked[hours]) - (hours_worked[hours] * .25);
//Statements
outFile<< "\n "<< employee_name << "\t" << hours << "\t" << rate << "\t" << gross << "\t" << bonus << "\t" << adjGross << endl;
}//IF Statement for hours less than 30
if(hours >= 30 && hours <=44)//IF Statement for hours between 35 and 44
{
//Local Declarations
double gross = (hourly_wage[rate] * hours_worked[hours]);
char bonus = 'N';
double adjGross = gross;
//Statements
outFile<< "\n "<< employee_name << "\t" << hours << "\t" << rate << "\t" << gross << "\t" << bonus << "\t" << adjGross << endl;
}//IF Statement for hours between 35 and 44
outFile.close();
return;
}//print_summary
//control passes back to the main function to system("pause")
--here is what in the input file--
Clinton 10.00 10
Lincoln 5.00 50
Washington 32.00 35
Kennedy 4.99 45
Nixon 10.00 25
--here is what I get in the output file--
The number of records read: -52
Maximum Pay Rate: Ì @ $-9.25596e+61
Minimum Pay Rate: Ì @ $-9.25596e+61
Average Pay: $-0
October 2009 Payroll:
Employee Name Hours Rate Gross Bonus Adjusted_Gross
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ 0 0 79508117989074987650738332503294079560541330881152498119070395440037888.00 N 214748365.00
$ g++ -W{all,extra,pedantic} foo.cpp
|| foo.cpp: In function ‘int main()’:
foo.cpp|14 col 9| warning: unused variable ‘gross’ [-Wunused-variable]
foo.cpp|15 col 9| warning: unused variable ‘bonus’ [-Wunused-variable]
foo.cpp|16 col 9| warning: unused variable ‘adjGross’ [-Wunused-variable]
|| foo.cpp: In function ‘bool process_employee(std::ifstream&)’:
foo.cpp|48 col 43| warning: right operand of comma operator has no effect [-Wunused-value]
|| foo.cpp: In function ‘void print_results(std::ofstream&)’:
foo.cpp|116 col 19| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|118 col 13| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|122 col 30| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|124 col 13| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
foo.cpp|128 col 34| warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function [-Wmaybe-uninitialized]
1 2
//foo.cpp|48 col 43| warning: right operand of comma operator has no effect [-Wunused-value]
if(inFile >> employee_name, hourly_wage, hours_worked)
to read into a variable use >> inFile >> a >> b >> c;
however `hourly_wage' is an array, so I'm not sure what you're trying to do there
> warning: ‘hourly_wage[<unknown>]’ may be used uninitialized in this function
this is a conceptual error
read about local variables and pass by reference