C++ 使函数在c+中正常工作+;
我发布的代码是Nell Dale书中的一个例子。我没有从函数C++ 使函数在c+中正常工作+;,c++,C++,我发布的代码是Nell Dale书中的一个例子。我没有从函数PrintResults获得预期结果。它显示所有值0 如何让它工作?将函数放在何处以给出准确的结果 我在ubuntu中使用code::Block。该问题计算草坪工程的账单并打印到文件中。输入文件格式为 #include <iostream> #include <fstream> #include <string> #include <iomanip> using na
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);
}
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
作废打开文件(ifstream和INFLE、ofstream和OUTILE);
/*打开文件以输入文件和输出文件的名称读取并打开
它是用来加工的*/
无效流程客户(ifstream&INFLE、ofstream&OUTILE、float hourlyRate);
//为填充中的所有客户编写账单
无效处理客户端(ifstream&inFile、ofstream&outFile、int TOTALLTIME、float hourlyRate);
//为客户写账单
无效获取和打印地址(如果流和填充、流和流出);
//从文件中读取地址并打印到输出文件
作废打印结果(int numberofBills、int totalMinutes、float hourlyRates);
//打印作业的总帐单平均时间和平均帐单的步骤
int main()
{
浮动小时利率;
河流充填;
出流孔的直径;
OpenFiles(填充、输出文件);
如果(!infle | |!outFile)
{
cout正如“一些程序员”在评论中提到的,问题在于ProcessAClient()
。当前构造方法的方式如下:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate)
通过传递输入文件、输出文件、输入文件中所有客户端的总时间以及要收费的小时费率来调用该方法。然后,总时间用于在printfresults()
中计算输入文件的摘要
但是,您的问题是变量totalTime
的作用域是ProcessAClient()
方法。您需要确保通过引用传递值。只需将ProcessAClient()
的两个定义更新为:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int& totalTime, float hourlyRate)
注意到totalTime
之前的&
,正如“一些程序员”在评论中提到的那样,问题在于ProcessAClient()
。该方法当前的构造方式如下:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int totalTime, float hourlyRate)
通过传递输入文件、输出文件、输入文件中所有客户端的总时间以及要收费的小时费率来调用该方法。然后,总时间用于在printfresults()
中计算输入文件的摘要
但是,您的问题是变量totalTime
的作用域是ProcessAClient()
方法。您需要确保通过引用传递值。只需将ProcessAClient()
的两个定义更新为:
void ProcessAClient(ifstream& inFile, ofstream& outFile, int& totalTime, float hourlyRate)
注意totalTime
之前的&
仔细查看ProcessAClient
函数。更具体地说,它的参数,以及如何传递它们。您是否尝试过使用调试器?如果“是”,您学到了什么?如果“否”,为什么不呢?能否详细说明一下您希望输入文件是什么样子的?它看起来像好像你要提供它,但实际上它不在问题中。仔细看看ProcessAClient
函数。更具体地说,它的参数,以及如何传递它们。你是否尝试过使用调试器?如果“是”,你学到了什么?如果“否”,为什么不?你能详细说明一下你期望输入文件的样子吗?看起来你要提供它,但实际上这不是问题。