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"

Recommended Answers

All 9 Replies

'\n'

if(code=='G')//;
         ifs >> gas; // ???
if ( gas_charge < gas_minimum )//;

Wow didn't even notice that...
Sometimes someone else just has to look at the code.
I'm still getting errors
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"

I don't know what the first one means, and can't find the other 2 errors.

EDIT
It now compiles for the first time. Thanks for your help.

If I want to write to a file, say...

Customer Number: 43454
How would I do it?

streams to manipulate files come in three basic types (at least for us beginners): streams dedicated to reading from files--- ifstream, streams dedicated to writing to files--- ofstream, and streams that can either read from or write to files depending on what mode it's in---fstream.

To use an ofstream you need to associate it with a file, just like you do with ifstream you used in your posted code. Once the file is opened the methods available to streams writing to files are essentially the same methods available for use with cout. So you can use put() or <<, etc. The one difference might be the write() method--- I'm not sure if that's available or not to ostreams such as cout.

I'm still confused on how to write out to file.

I'm pretty sure this is way wrong, but I've searched and searched with no avail how to write out to file correctly. It looks like everyone does it differently...

// Report customer bill
   ofstream fout ("Customer_Bill");
   if(!fout) {
      cout << "Cannot open output file.\n";
      return 1;
             }
   fout << "Customer Number:           " << cust_number << endl;
   fout << "Customer Name:             " << name << endl;
   fout << "Customer Addresss:         " << address << endl;
   fout << "Water Used:                " << water << endl;
   fout << "Water Charge:              " << water_charge << endl;
   fout << "Electricity Used:          " << electric << endl;
   fout << "Electricity Charge:        " << electric_charge << endl;

   if (code=='G')
      fout << "Gas Used:                  " << gas << endl;
      fout << "Gas Charge:                " << gas << endl;

   fout << "Total Bill:                " << total_bill << endl;

// Update summary data
   num_customers = num_customers + 1;
   total_water = total_water + water;
   total_electric = total_electric + electric;
   total_gas = total_gas + gas;
   total_water_revenue = total_water_revenue + water_charge;
   total_electric_revenue = total_electric_revenue + electric_charge;
   total_gas_revenue = total_gas_revenue + gas_charge;

   // Read the next customer number

   }

// Report summary data for all customers
   total_revenue = total_water_revenue + total_electric_revenue + total_gas_revenue
   fout << "Number of Customers:       " << num_customers << endl;
   fout << "Total Water Used:          " << total_water << endl;
   fout << "Total Water Revenue:       " << total_water_revenue << endl;
   fout << "Total Electricity Used:    " << total_electric << endl;
   fout << "Total Electricity Revenue: " << total_electric_revenue << endl;
   fout << "Total Gas Used:            " << total_gas << endl;
   fout << "Total Gas Revenue:         " << total_gas_revenue << endl;
   fout << "Total Revenue:             " << total_revenue << endl;


   cout << "This program will read input from file 'customers' " << endl;
   cout << "and output will be written to file 'Customer_Bill'." << endl;

That's the very end of my code, it starts where I left off above...

I know fout isn't write, but I had to put something so I could finish writing...

>>It looks like everyone does it differently

It's called flexibility and it can be maddening, liberating, or both at any given moment.

I would an extension type to the file name Customer_Bill but otherwise the syntax regarding the ofstream looks okay to me.

It's wise to close the file once you are done with it, too.

In:

if (code=='G')
      fout << "Gas Used:                  " << gas << endl;
      fout << "Gas Charge:                " << gas << endl;

only the Gas Used line is part of the if statement. If you want the Gas Charge statement to be included in the if statement then throw both lines after the if statement in a pair of curly brackets, {}. Note that you output the same variable on both lines. Is that what you want?

Thanks for the help about the if statement. I fixed that now, didn't realize it as i was writing it all very fast.

Now I have another problem...

// Report summary data for all customers
   total_revenue = total_water_revenue + total_electric_revenue + total_gas_revenue;
   fout << "Number of Customers:       " << num_customers << endl;
   fout << "Total Water Used:          " << total_water << endl;
   fout << "Total Water Revenue:       " << total_water_revenue << endl;
   fout << "Total Electricity Used:    " << total_electric << endl;
   fout << "Total Electricity Revenue: " << total_electric_revenue << endl;
   fout << "Total Gas Used:            " << total_gas << endl;
   fout << "Total Gas Revenue:         " << total_gas_revenue << endl;
   fout << "Total Revenue:             " << total_revenue << endl;

In that code...for some reason I'm getting an error. That's my only error when I try to compile

The error is in line 157, which is

fout << "Number of Customers:       " << num_customers << endl;

Why would I get an error only there? It says:
main.cpp: In function `int main()':
main.cpp:158: error: `fout' was not declared in this scope
It actually switches from line 157 to 158 after I try to compile it each time.

And how do I correctly close the opened and written to files? I have tried various ways and it doesn't work out. I made "Customer_Bill" now "Customer_Bill.txt"


EDIT:
Also, how do I read the next customer's number?

// Read the next customer number

   }

I have an algorithm telling me to, but I don't know how without getting an infinite loop

>> Now I have another problem...
I suspect that your code goes something like this;

[B]{[/B]  // <- start of a block (for some reason)
    ofstream fout;
    ... code ...
[B]}[/B]  // <- end of the block ('fout' does not exist anymore at this point)
... code ...
// Report summary data for all customers
   total_revenue = total_water_revenue + total_electric_revenue + total_gas_revenue;
   // trying to use non-existing 'fout' here
   fout << "Number of Customers:       " << num_customers << endl;
... code ...

Okay that is fixed and it compiles correctly, but I still do not know how to read the next input for the next customer. It loops infinitely no matter what I try.

What is the format of the input file (i.e. what fields and in which order)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.