2017-08-04 6 views
0

私が投稿しているコードは、Nell Daleの本の例です。関数PrintResultsから期待される結果が得られません。すべての値が表示されます。機能をC++で正しく機能させる

どのように動作させるのですか?どこに正確な結果を与えるための関数を置くのですか?

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

それはうまくいった。ありがとうございました –

関連する問題