我一直是这个网站的潜伏者,现在我需要帮助在这段代码中实现数组。我不知道如何将代码添加到这里,所以这里是
#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;
}
答案 0 :(得分:0)
There are two ways to add arrays in C++. The first way is to specify the type of the array (int, double, float, char, string) and then specify the length of the array and type all the values until you get to the length. For example int arr[5] = {1, 2, 3, 4, 5};
For the other way you don't need to specify the length so that means you can put as many elements in the array as you like but you still need the data type and variable name like int arr[] = {1, 2, 3, 5, 8, 13, 21, 34};
. Remember though in programming we start counting from 0.