0

Hello. In my assignment, I must use nested loops to write to and read from a file. We have not yet learned functions or arrays. The program should calculate and display total rent collected, average rent collected, the highest paying complex, and a warning if zero paid by a complex. Everything seems to be working except I can't figure out how to calculate total just for each complex. I'm only getting overall total. Tried all sort. Any pointers would be appreciated. Thank you very much.

#include <stdafx.h> 
#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream>
using namespace std;

int main()
{
    ofstream outputFile;
    int rentMonths, numComplex, counter = 0;
    double rentCollected, totalRent = 0, averageRent,  complexTotal, highestRent;
    bool zeroPayment = false;
    string complexName, noPay;

     // Create and open output file
    outputFile.open("Rent.txt");

    //Get user's input and write to file
    cout << "How many complexes have paid rent? "; 
    cin >> numComplex;
    if (numComplex <= 0)
    {
            cout << "Invalid; please enter a positive number: " << endl;
        cin >> numComplex;
    }
     cout << "How many months of rent for each complex? "; 
     cin >> rentMonths;

    //Input each complex's rent paid
    for (int complex = 1; complex <= numComplex; complex++)
    {
        cout << "Enter complex name: ";
        cin >> complexName;
        outputFile << complexName << " ";

        double total = 0; // Initialize accumulator
        for (int month = 1; month <= rentMonths; month++)
        {
            cout << "Enter the rent collected for month " << month << ": " << endl;
            cin >> rentCollected;
            outputFile << rentCollected << " ";
            total = total + rentCollected;
        }
        outputFile << endl;
      }
      outputFile.close();

      // Read rent data from input file
     ifstream inputFile;
     inputFile.open("Rent.txt");
     cout << fixed << showpoint << setprecision(2);
     cout << "\nRent collected for each complex \n\n";
     if (inputFile)
     {
        while (inputFile >> complexName)
        {
            cout << complexName << "\t";
            double total = 0; // Initialize accumulator
            for (int month = 1; month <= rentMonths; month++)
            {
                inputFile >> rentCollected;
                cout << rentCollected << "\t";
                if (rentCollected == 0)
                {
                    zeroPayment = true;
                    noPay = complexName;
                }
                totalRent = totalRent + rentCollected;
               }

            cout << endl;
            }

      }
         if (zeroPayment)
            cout << "\n\nWARNING: Complex " << noPay << "     made a payment of 0!\n\n";
           cout << "The total rent collected is: $" << totalRent << "\n\n";
           cout << "The average rent collected is: $" << totalRent / numComplex << "\n\n";
           cout << "The complex with the highest amount of rent is:     \n\n";
    return 0;
}
2
Contributors
2
Replies
19
Views
7 Months
Discussion Span
Last Post by AssertNull
0

What's supposed to go into the output file? Just each month's rent, but no totals? It appears to be that way now. Assuming that is correct, I'm seeing a variable called total used in lines 37 and 43 that appears to calculate the total rent for each complex. However, I don't see that total written to the file or the screen, so why bother calculating it?

I see a line 59 that seems to correspond with line 37. I see no corresponding line 43 below line 59. If you are calculating the total for each complex that you read from the file, it needs to be there. Use lines 37 and 43 as your model. If you need to display the total for each complex to the screen, simply diplay the variable total in line 72 before endl. Pretty sure that's what you are going for.

As for the complex with the highest amount of rent, you need two more variables as far as I can tell.

  1. double highestRent
  2. string complexWithHighestRent

Initialize highestRent to 0 right before line 56.

On line 71, compare total with highestRent. If total is bigger than highestRent, make total equal highestRent and make complexWithHighestRent equal complexName. Then display complexWithHighestRent on line 80.

Edited by AssertNull

0

Okay, looks like you have highestRent at the top, so no need to declare it again. Just initialize it to 0. It will be overwritten later.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.