I'm writing a program to input a file called customers and output it as bills, but I'm not sure how to use ifstream. I've searched for quite a while and haven't really found anything useful. I'm probably doing it wrong though. Still new at this.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
// Program Description
// This program will read input from file 'customers'
// and output will be written to file 'bills'.
const double water1 = 0.60; // 60 cents per 100 cu. ft. for the first water category
const double water2 = 0.40; // 40 cents per 100 cu. ft. for the second water category
const double water3 = 0.32; // 32 cents per 100 cu. ft. for the third water category
const double electric1 = 0.05; // 5 cents per kWh for the first electricity category
const double electric2 = 0.03; // 3 cents per kWh for the second electricity category
const double electric3 = 0.04; // 4 cents per kWh for the third electricity category
const double electric4 = 0.06; // 6 cents per kWh for the forth electricity category
const double gas1 = 1.50; // $1.50 per 1000 cu. ft. for the first gas category
const double gas2 = 1.40; // $1.40 per 1000 cu. ft. for the second gas category
const double gas3 = 1.55; // $1.55 per 1000 cu. ft. for the third gas category
const double water_minimum = 3.00; // Minimum charge for the water is $3.00
const double electric_minimum = 7.00; // Minimum charge for the electricity is $7.00
const double gas_minimum = 6.00; // Minimum charge for the gas is $6.00
int main()
{
// Variables
int cust_number; // Customer number
string name; // Customer name
string address; // Customer address
char code; // Determines type of customer
int water; // Input of amount of water used
int electric; // Input of amount of electricity used
int gas; // Input of amount of gas used
double water_charge; // Amount customer being charged for water
double electric_charge; // Amount customer being charged for electric
double gas_charge; // Amount customer being charged for gas
double total_bill; // Total bill for a single customer
int total_water; // Total amount of water used by all customers
int total_electric; // Total amount of electric used by all customers
int total_gas; // Total amount of gas used used by all customers
double total_water_revenue; // Amount all customers are charged for water
double total_electric_revenue; // Amount all customers are charged for electricity
double total_gas_revenue; // Amount all customers are charged for gas
double total_revenue; // Total revenue for all customers
int num_customers; // Number of customers processed
// Initial totals
num_customers = 0;
total_water = 0;
total_electric = 0;
total_gas = 0;
total_water_revenue = 0;
total_electric_revenue = 0;
total_gas_revenue = 0;
// Loop to read, process and report each customer
ifstream ifs ("customers");
ifs>>cust_number;
while (cust_number!=0);
{
ifs.ignore(80,'/n');
getline(ifs,name);
getline(ifs,address);
ifs>>code;
ifs>>water;
ifs>>electric;
if(code=='G');
ifs(gas);
}
// Calculate water charge
if (water <= 10000)
water_charge = water/100 * water1;
else if (water <= 100000)
water_charge = 100*water1 + (water-100000)/100 * water3;
else
water_charge = 100*water1 + 900*water2 + (water-100000)/100 * water3;
// Calculate electric charge
if (electric <=500)
electric_charge = electric * electric1;
else if (electric = 1500)
electric_charge = 500*electric1 + (electric-500)*electric2;
else if (electric <= 3500)
electric_charge = 500*electric1 + 1000*electric2 + (electric-1500)*electric3;
else
electric_charge = 500*electric1 + 1000*electric2 + 2000*electric3 + (electric-35000)*electric4;
if (electric_charge < electric_minimum);
electric_charge = electric_minimum;
// Calculate gas charge
if (code=='G')
if (gas <=50000)
gas_charge = gas/1000 * gas1;
else if (gas <= 100000)
gas_charge = 50 * gas1 + (gas-50000)/1000 * gas2;
else
gas_charge = 50* gas1 + 50*gas2 + (gas-100000)/1000 * gas3;
if (gas_charge < gas_minimum);
gas_charge = gas_minimum;
else
{ gas = 0;
gas_charge = 0;
}
// Calculate total_bill
total_bill = water_charge + electric_charge + gas_charge;
// Report customer bill
return 0;
}
I'm by no means finished, but how do I write the following part correctly?
ifstream ifs ("customers");
ifs>>cust_number;
while (cust_number!=0);
{
ifs.ignore(80,'/n');
getline(ifs,name);
getline(ifs,address);
ifs>>code;
ifs>>water;
ifs>>electric;
if(code=='G');
ifs(gas);
Errors I get are
main.cpp:71:21: warning: multi-character character constant
main.cpp: In function `int main()':
main.cpp:78: error: no match for call to `(std::ifstream) (int&)'
main.cpp:112: error: expected primary-expression before "else"
main.cpp:112: error: expected `;' before "else"