使函数在C++中正常工作
问题描述:
我发布的代码是来自Nell Dale一书的一个例子。我没有得到预期的功能PrintResults
的结果。它显示所有值0.使函数在C++中正常工作
如何使它工作?在哪里把功能给出确切的结果?我在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);
}
答
正如在“某程序员家伙”的评论中提到的,问题出在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
指出。
+0
工作。谢谢 –
仔细看一下'ProcessAClient'函数。更具体地说它的论点,以及你如何通过它们。 –
您是否尝试过使用您的调试器?如果'是',你学到了什么?如果'不'为什么不是? –
您可以详细说明您期望输入文件的外观吗?它看起来像你要提供它,但它不是真正的问题。 – Jvinniec