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;
}