Comparing Values from external file

I am a beginner and this is a homework assignment, so I don't need full blown out answers! I know my code probably looks like a mess, so thank you for understanding. I have written almost all of my code for my program with no errors yet, but I need help figuring out how to read the number values from a text file, calculate totals for each "group" and compare them so I can display which group had the highest total in line 80. I have read about arrays, but I am not that far into my class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	int numComplex, numMonths;
	double rentCollected, avgMonthRent, rent;
	string complexName;
	
	cout << setprecision(2) << fixed;

	cout << "Enter the number of Complexes: ";
	cin >> numComplex;

	cout << "Enter the number of months rent was collected: ";
	cin >> numMonths;
	cout << endl;

	ofstream outputFile;
	outputFile.open("ComplexIncome.txt");

	outputFile << "Complex Monthly Rent Collected per Complex\n";
	outputFile << "------------------------------------------\n";

	rentCollected = 0;

	for (int complex = 1; complex <= numComplex; complex++)
	{
		cout << endl;
		cout << "Enter the name of the complex: ";
		cin >> complexName;
		outputFile << complexName << "\t";

		for (int month = 1; month <= numMonths; month++)
		{
			cout << "Enter the rent collected in month " << month << " for " << complexName << ": $";
			cin >> rent;

			outputFile << setprecision(2) << fixed;

			outputFile << rent << "\t";
			if (rent <= 0)
			{
				cout << endl;
				cout << "Warning! If ZERO rent was collected, a company audit will ensue!\n";

				system("pause");
			}
			rentCollected += rent;
		}
		outputFile << "\n";
	}

	outputFile.close();
	cout << endl << endl;
	
	string line;
	ifstream inputFile;
	inputFile.open("ComplexIncome.txt");

	if (inputFile.is_open())
	{
		while (getline(inputFile, line))
		{
			cout << line << endl;
		}
		inputFile.close();
		cout << endl << endl;
	}
	else
		cout << "Error opening file\n";

	avgMonthRent = rentCollected / numMonths;

	cout << "The total rent collected for the company: $" << rentCollected << endl;
	cout << "The average monthly rent collected for the company: $" << avgMonthRent << endl << endl;
	cout << "Complex " << "" << "collected the most rent with $" << "" << " collected!\n\n";
	
	if (rent <= 0)
	{
		cout << "Warning! One of the complexes submitted ZERO rent for one of the months!\n\n";
	}

	system("pause");
	return 0;
}


My text file contains a name first followed by the numerical values, so is it possible to calculate the numerical totals of each group while still associating them with the corresponding name? Thank you for your time.
Last edited on
so is it possible to calculate the numerical totals of each group while still associating them with the corresponding name? Thank you for your time.

can you do that on paper? I don't know exactly what you want to total... if one name has 5 values, you can add the 5 values together and associate that to the name, yes. If you have 10 people each with one value and total it, the 'person' info is lost, its either associated to the group of 10 or to no one, but its unclear.

Not sure exactly what you are dealing with... it could be as simple as read in group name, read in value. read in next group name, it is same as last one? if so, add to running total for that group, if not, new group. Total up second group, is that more than first? if yes, biggest total is now the new group, else it was the first group. repeat for third, fourth,... nth group.
does THAT sound like what you are being asked to do? ^^^ At any one time, you only have to hold 2 groups in your variables: the biggest one (its name and total) and the current one being processed. If this is what you need to do, see if you can now that you have direction..
Last edited on
Thank you for a response.

I have to get user input for how many apartments there are[line 15&16], and how many months of rent that were collected [line 18&19]. Write those inputs to a text file, then later in the program read them from the text file.

On line 80, I need to display which apartment had the highest amount collected.
If it was a set amount of apartments and months this would be easier for me.

My text file looks like this (essentially):

Complex Monthly Rent Collected per Complex
--------------------------------------------------
Main 2000.00 1000.00 3000.00 2000.00 2000.00
Clay 3000.00 5000.00 4000.00 5000.00 4000.00
King 2000.00 2000.00 4000.00 2000.00 0.00

So I suppose my question is, how do I sum and compare the values of each line so I can output the name and total of the highest earning complex in the appropriate places on line 80?

Is an array the only way of doing this?
yes, several of them.
do you know struct? basic idea is to make a user defined type that holds the info you need from the file (which does not appear to be necessary, actually) and to make an array of *those*

rough pseudo code ... there is a bit to do to set this up though...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct data
{
   string complex_name;
   double rent[some_max_number];
};

data d[number_of_complex];
for(...)
{
 file >> d[complex_id].complex_name;
  for(...some_max_number...)
   {
        file >> d[complex_id].rent[index]; //if each line has different number of numbers, when you run out of values on your line, you will want to zero out the rest of the rent[] array so sum of zeros == 0. 
   }
}



alternately you can just, as you process the file, total up the values and store the single total. Or as I said above, no array, just track the biggest value as you go, and print it at the end.

there are many other ways to do this. this is an overly simple direct approach.

do you see it?
open file.
read main. main is now the biggest (first one seen logic).
main's total is 10k.
read... clay. clay's total is 21k, clay is now biggest. save 'clay' and '21k'
read .. king. king's total is 10k. it is not bigger.
... end of file.
print: clay was biggest one found at 21k.

we didn't have to store main and king after we found clay, we only ever stored 1 item, and compared it to the current item each time, at the end, you know the answer.
Last edited on
Thanks you. I see now that my logic was off sending down a more complicated path. So would I need to use something like another "if statement" or a "while statement" in my second "for statement" to separately calculate and compare the values?

I appreciate your help.
it seems likely. perhaps something like getline from file (getline is a function..) and break that up based off spaces or whatever, total it up (I think this is the loop you are asking about) and follow that loop with a (if this new value > the last value...) logic... right?

There are a lot of ways to do it. Give it a try and ask again if you get stuck (with fresh code).
Topic archived. No new replies allowed.