Reputation: 1
The code i am posting is an example from the book of Nell Dale. I am not getting expected result from the function PrintResults
. It shows all values 0.
How to make it work? Where to put the function to give the exact result?
I'm using code::Block
in ubuntu
. The problem calculates the bill for lawn work and print to a file. The input file format is.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Function prototypes
void OpenFiles(ifstream& inFile, ofstream& outFile);
/* Open file reads in the name of input files & output files and open
it for processing*/
void ProcessClients(ifstream& inFile, ofstream& outFile, float hourlyRate);
// write bill for all the clients in the infile
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate);
//For writing the bill of a client
void GetAndPrintAdress(ifstream& inFile, ofstream& outFile);
// Reads adress from the file and prints to outfile
void PrintResults(int numberofBills, int totalMinutes, float hourlyRates);
// to print total bill average time for job and average bill
int main()
{
float hourlyRate;
ifstream inFile;
ofstream outFile;
OpenFiles(inFile, outFile);
if(!inFile || !outFile)
{
cout << "Error opening files"<< endl;
return 1;
}
cout << "Enter Hourly Rate"<< endl;
cin >> hourlyRate;
ProcessClients(inFile, outFile, hourlyRate);
inFile.close();
outFile.close();
return 0;
}
//************************************************************************************//
void OpenFiles(ifstream& inFile, ofstream& outFile)
{
string inFileName;
string outFileName;
cout << "Enter the name of infile" <<endl;
cin >> inFileName;
inFile.open(inFileName.c_str());
cout << "Enter the name of out File"<< endl;
cin >> outFileName;
outFile.open(outFileName.c_str());
outFile << "Billing for clients on file "<< inFileName <<endl;
outFile << fixed;
}
//******************************************//
void ProcessClients(ifstream& inFile, ofstream& outFile, float hourlyRate)
{
int totalTime=0;
int numberofBills=0;
string name;
getline(inFile, name);
while(inFile)
{
outFile<< name<< endl;
ProcessAClient(inFile, outFile, totalTime, hourlyRate);
numberofBills++;
getline(inFile, name);
}
PrintResults(numberofBills, totalTime, hourlyRate);
}
//***************************************************
void PrintResults(int numberofBills, int totalMinutes, float hourlyRate)
{
cout << "minutes: "<<totalMinutes<<endl;
float minutes = static_cast<float>(totalMinutes);
cout << "Total amount billed this month is "<< minutes / 60.0 * hourlyRate<<endl;
cout << "Average time worked per job is "<< minutes / float(numberofBills) / 60.0<< endl;
cout << "Average customer bill "<< minutes / 60.0*hourlyRate / float(numberofBills)<< endl;
}
//****************************************************************************
void GetAndPrintAdress(ifstream& inFile, ofstream& outFile)
{
string line;
getline(inFile, line);
outFile<< line<<endl;
getline(inFile, line);
outFile<<
line<<endl<<endl;
}
//***********************************************************************************
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate)
{
int time=0;
int hours;
int minutes;
float cost;
int numberofJobs;
GetAndPrintAdress(inFile, outFile);
inFile >> numberofJobs;
outFile << "Number of jobs "<< numberofJobs<< endl;
for(int count=1; count<=numberofJobs; count++)
{
inFile >> hours>> minutes;
time =hours*60+minutes+time;
outFile << "Job "<< count<< ":"<< hours<< " hours and "<< minutes<< " minutes "<< endl;
}
cost=static_cast<float>(time)/60.0*hourlyRate;
totalTime=totalTime+time;
outFile << "Amount of Bill "<< setprecision(2)<< cost<<endl<<endl;
string skip;
getline(inFile, skip);
}
Upvotes: 0
Views: 140
Reputation: 636
As mentioned in the comment by "Some programmer dude" the issue is in ProcessAClient()
. The way the method is currently constructed is as follows:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate)
and the method is called by passing it the input file, the output file, the total time for all the clients in the input file, and the hourly rate to be charged. The total time is then used to compute the summary of the input file in PrintResults()
.
Your problem, however, is that the variable totalTime
is scoped to the ProcessAClient()
method. You need to make sure to pass the value by reference. Simply update the two definitions of ProcessAClient()
to be:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int& totalTime, float hourlyRate)
noting the &
before totalTime
.
Upvotes: 2