Project question! (how to sort)

Hey everyone! I'm here to ask for a little help and/or direction on a C++ project for a class. Basically, the project is asking me to sort a bunch of data based off of a meter number (the project is taking the mask of a meter log type deal). I have a previous iteration of the code that fulfills necessary formulas and whatnot that are being asked for in the full summary, but I'm not sure how to start sorting the data. The file is reading numerical input from a text file. Is there a way to share a PDF to show what's being asked for?
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 ******************************************************************************
 **                                                                          **
 **             WaterWorks Utilities Company     PROPRIETARY DATA            **
 **                                                                          **
 **       This document contains proprietary technical, commercial, or       **
 **       financial trade secret data or information confidential to         **
 **       WaterWorks Utilities Company. The information/data or any of the   **
 **       ideas represented herein may not be disclosed to others, nor       **
 **       copied, manufactured, or used by others without prior written      **
 **       permission of WaterWorks Utilities Company.                        **
 **                                                                          **
 **       Copyright (c) 2019 by WaterWorks Utilities Company                 **
 **               All Rights Reserved.                                       **
 **                                                                          **
 **          Any reproduction of this data must be                           **
 **           marked with the restriction herein.                            **
 **                                                                          **
 ******************************************************************************

 Revision history:
 2/4/19     ---------------  Initial release
 2/21/19    "             "  Revised the program so that it stores the requested information in a text file
 3/29/19    "             "  Revised the program so that it reads information from a text file 
 4/30/19    "             "  Revised program to sort and order according to meter number
*/

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    
	// Program variables
	float totalGalUsage;                // the total gallon usage for all three months  
	float avgGalUsageUser;              // the average gallon usage per 3 months per meter
	float avgGalUsageGeneral;           // the product of the average water usage per square foot and the residence size
	const float SQFOOTAVGUSAGE = 2.095; // the constant of the average water usage per square foot
	
	// Variables neccesary to calculate summary values
	double grandTotal = 0;              // the grand total of all water usage across all meters
	double avgConMeter;                 // the average water consumption per meter
	double avgConMonth;                 // the water consumption per month
	int i = 0;                          // a counter that counts the number of meters per program
	
	// Opening MeterLog.txt in order to begin inputting its data
	ifstream inputFile;
	inputFile.open("MeterLog.txt");
	
	// If statement to verify that the input file was opened (if the file was opened, the program runs as normal)
	if (inputFile.is_open())
    {
    
    // Declaring variables that will be inputted from file
    int meterMonth;         // the month displayed in the meter
    int meterDay;           // the day displayed in the meter
    int meterYear;          // the year displayed in the meter
    int meterNumber;        // the meter number
    double residenceSize;   // the residence size per meter
    double galMonth1;       // the gallon usage per month 1 per meter 
    double galMonth2;       // the gallon usage per month 2 per meter
    double galMonth3;       // the gallon usage per month 3 per meter

    // While the file is inputting values, the program will loop
    while(inputFile >> meterMonth >> meterDay >> meterYear >> meterNumber >> residenceSize >> galMonth1 >> galMonth2 >> galMonth3)
    
        {
            
        // Calculation for total usage, user average, and general average
        totalGalUsage = galMonth1 + galMonth2 + galMonth3;
        avgGalUsageUser = totalGalUsage / 3;
        avgGalUsageGeneral = ( residenceSize * SQFOOTAVGUSAGE );
        
        // Calculation for the grand total located in the summary
        grandTotal += totalGalUsage;
        i++;
    
        }

    // Calculations for the summary values
    avgConMeter = grandTotal / i;
    avgConMonth = grandTotal / (i*3);
    
    // Displaying the summary
    cout << "----------------------------------------------" << endl;
    cout << "                    Summary                   " << endl;
    cout << "----------------------------------------------" << endl;
    cout << fixed << setprecision(2) << "Grand Total Consumption: " << grandTotal << endl; // sum of total gallon usage
    cout << fixed << setprecision(2) << "Average Consumption Per Meter: " << avgConMeter << endl; // total gallon usage divided by number of reports
    cout << fixed << setprecision(2) << "Average Consumption Per Month: " << avgConMonth << endl; // total gallon usage divided by (number of reports, times 3)
    
    }
    
    // "else" if the input file fails to open
    else
        {
            cout << "Error opening file";
        }
	
    // Closing the file
    inputFile.close();
    return 0;
    
}
Last edited on
Use one of the sort algorithms found in <algorithm>.

BTW, according to your legal block, you are breaking the terms of access by sharing it online. It appears to be a fake company; hence, fake legal block.

Were it a real thing, you could be fired.
before you can sort data, you need something to sort.

you are reading the file, processing the data you found, and then throwing that data away. If you wanted to sort that data or something else, you need to store it so you have all the values in memory (a vector, an array, etc of the data, something like that).

that is, if you had in memory 3,2,1 you can sort that to 1,2,3. But if you read 3 into x, and then read 2 into x, and then read 1 into x, the 3 and 2 are gone, you can't sort that.
Last edited on
Topic archived. No new replies allowed.