2017-07-06 64 views
-6

我一直是這個網站的潛伏者,現在我需要在這個代碼中實現數組的幫助。我不知道如何將代碼添加到這裏,所以這裏的我將如何實現數組到這個代碼

#include <iostream> 
#include <fstream> 
#include <iomanip> 
using namespace std; 
//Named constants – residential customers 
const double RES_BILL_PROC_FEES = 4.50; 
const double RES_BASIC_SERV_COST = 20.50; 
const double RES_COST_PREM_CHANNEL = 7.50; 
//Named constants – business customers 
const double BUS_BILL_PROC_FEES = 15.00; 
const double BUS_BASIC_SERV_COST = 75.00; 
const double BUS_BASIC_CONN_COST = 5.00; 
const double BUS_COST_PREM_CHANNEL = 50.00; 
int main() 
{ 
    //Variable declaration 
    int accountNumber; 
    char customerType; 
    int numOfPremChannels; 
    int numOfBasicServConn; 
    double amountDue; 
    int rescount = 0, buscount = 0; 
    double ressum = 0.00, bussum = 0.00; 
    double resavg, busavg; 
    ifstream fin; 
    ofstream fout; 
    //open input file 
    fin.open("custin.txt", ios::in); 
    if (fin.fail()) { 
     cout << "Unable to open file " << endl; 
     exit(0); 
    } 

    //open ouput file 
    fout.open("custout.txt", ios::out); 
    fout << fixed << showpoint; 
    fout << setprecision(2); 
    fout << "This program computes a cable " 
     << "bill." << endl; 
    //read from input file 
    while (!fin.eof()) { 
     //read customer type from file 
     fin >> customerType; 
     //read account number from file 
     fin >> accountNumber; 

     //If customer type is residential 
     if (customerType == 'r' || customerType == 'R') { 
      //read number of prem channels from file 
      fin >> numOfPremChannels; 
      amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL; 

      //write into output file 
      fout << "Account number: " << accountNumber << endl; 
      fout << "Amount due: $" << amountDue << endl 
       << endl; 
      //compute running average of residential cusomer's amount due 
      rescount += 1; 
      ressum += amountDue; 
      resavg = ressum/rescount; 
     } 

     //If customer type is business 
     else if (customerType == 'b' || customerType == 'B') { 
      //read number of service connection from file 
      fin >> numOfBasicSevConn; 

      //read number of prem channels from file 
      fin >> numOfPremChannels; 
      if (numOfBasicServConn <= 10) 
       amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; 
      else 
       amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL; 
      fout << "Account number: " << accountNumber << endl; //Step 7f 
      fout << "Amount due: $" << amountDue << endl 
       << endl; //Step 7f 

      //compute running average of business cusomer's amount due 
      buscount += 1; 
      bussum += amountDue; 
      busavg = bussum/buscount; 
     } 
     else 
      fout << "Invalid customer type.in " << accountNumber << endl; //Step 8 
    } 
    fout << endl 
     << "Average Due for residential Customer " << resavg << endl; 
    fout << endl 
     << "Average Due for Business Customer " << busavg << endl; 
    fin.close(); 
    fout.close(); 
    return 0; 
} 
+4

如果你是這個網站的潛伏者,你會知道你需要提供[mcve],具有確切的問題描述,而不是鏈接到外部網站。 –

+0

代碼進入問題,而不是一些非現場文本文件託管服務。 – tadman

+0

你需要在哪裏添加數組?出於什麼目的? –

回答

0

有兩種方法可以在C++中添加數組。第一種方法是指定數組的類型(int,double,float,char,string),然後指定數組的長度並輸入所有值直到達到長度。例如int arr[5] = {1, 2, 3, 4, 5};對於另一種方式,您不需要指定長度,這意味着您可以根據需要將盡可能多的元素放入數組中,但仍需要數據類型和變量名稱,如int arr[] = {1, 2, 3, 5, 8, 13, 21, 34};。請記住,雖然在編程中我們從0開始計數。

+0

感謝這幫助我,並對另一個網站的鏈接感到抱歉,由於某種原因,我無法發佈整個代碼。 – aprz

相關問題