2017-08-04 65 views
0

我發佈的代碼是來自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); 
    } 
+3

仔細看一下'ProcessAClient'函數。更具體地說它的論點,以及你如何通過它們。 –

+1

您是否嘗試過使用您的調試器?如果'是',你學到了什麼?如果'不'爲什麼不是? –

+0

您可以詳細說明您期望輸入文件的外觀嗎?它看起來像你要提供它,但它不是真正的問題。 – Jvinniec

回答

2

正如在「某程序員傢伙」的評論中提到的,問題出在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

工作。謝謝 –