我发布的代码是 Nell Dale 书中的示例。我没有从函数 PrintResults
中得到预期的结果。它显示所有值 0。
如何让它发挥作用?将函数放在哪里才能给出准确的结果?
我在 ubuntu
中使用 code::Block
。该问题计算草坪工作的账单并打印到文件中。输入文件格式为。
#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);
}
最佳答案
正如“Some programmer dude”在评论中提到的,问题出在 ProcessAClient()
中。目前构造方法的方式如下:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate)
并且通过将输入文件、输出文件、输入文件中所有客户的总时间以及要收费的小时费率传递给该方法来调用该方法。然后,总时间用于计算 PrintResults()
中输入文件的摘要。
但是,您的问题是变量 totalTime
的范围限定为 ProcessAClient()
方法。您需要确保通过引用传递值。只需将 ProcessAClient()
的两个定义更新为:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int& totalTime, float hourlyRate)
注意 totalTime
之前的 &
。
关于c++ - 使函数在 C++ 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45501117/