SOS: Moving data to a big void print function

"You'll need to print out the yearly profits in generateReport() along with everything else (that's the only way to get it to appear all together) - so make sure you run calculateYearlyProfits() before calling generateReport() (then it'll have the data ready to print!)"

I have no idea what to keep in main() or move to the generateReport() to print a table that lines up properly. Thank you.



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int MAX_STR = 100;

int loadData(ifstream& fin, string rideNames[], double q1Earnings[], double q2Earnings[],
             double q3Earnings[], double q4Earnings[]);

double calculateQuarterlyEarnings(int rideCount, const double earnings[]);

int profitable(int rideCount, const double earnings[]);

void calculateYearlyProfits(int rideCount, const double q1Earnings[], const double q2Earnings[],
                            const double q3Earnings[], const double q4Earnings[], double yearly[]);

void generateReport(int rideCount, const string rides[], const double q1Earnings[],
                    const double q2Earnings[], const double q3Earnings[], const double q4Earnings[],
                    const double yearly[]);

int main()
{
    int rideCount;
    double q1Earnings[MAX_STR], q2Earnings[MAX_STR], q3Earnings[MAX_STR], q4Earnings[MAX_STR],
           yearly[MAX_STR];
    ifstream fin;
    ofstream outputFile;
    string rideNames[MAX_STR], inputFileName, outputFileName;

    cout << "What file should be opened for processing? " << endl;
    // cin >> inputFileName;
    fin.open("report.txt");
    outputFile.open("out.txt");

    cout << setprecision(2) << fixed;
    cout << setw(95)
         << "RIDE NAME    Q1 Earnings    Q2 Earnings    Q3 Earnings    Q4 Earnings         YEARLY"
         << endl;
    cout << "**************************************************************************************"
            "*********"
         << endl;
    int rides = loadData(fin, rideNames, q1Earnings, q2Earnings, q3Earnings, q4Earnings);
    for (int i = 0; i < rides; i++)
    {
        cout << setw(20) << right << rideNames[i] << setw(15) << right << q1Earnings[i] << setw(15)
             << right << q2Earnings[i] << setw(15) << right << q3Earnings[i] << setw(15) << right
             << q4Earnings[i] << endl;
    }
    calculateYearlyProfits(rides, q1Earnings, q2Earnings, q3Earnings, q4Earnings, yearly);
    cout << "**************************************************************************************"
            "*********"
         << endl;
    cout << setw(19) << right << "TOTALS:" << setw(16) << right
         << calculateQuarterlyEarnings(rides, q1Earnings) << setw(15) << right
         << calculateQuarterlyEarnings(rides, q2Earnings) << setw(15) << right
         << calculateQuarterlyEarnings(rides, q3Earnings) << setw(15) << right
         << calculateQuarterlyEarnings(rides, q4Earnings) << setw(15) << right
         << (calculateQuarterlyEarnings(rides, q1Earnings)
             + calculateQuarterlyEarnings(rides, q2Earnings)
             + calculateQuarterlyEarnings(rides, q3Earnings)
             + calculateQuarterlyEarnings(rides, q4Earnings))
         << endl;
    cout << setw(19) << right << "Profitable Rides:" << setw(14) << right
         << profitable(rides, q1Earnings) << '/' << rides << setw(13) << right
         << profitable(rides, q2Earnings) << '/' << rides << setw(13) << right
         << profitable(rides, q3Earnings) << '/' << rides << setw(13) << right
         << profitable(rides, q4Earnings) << '/' << rides;

    fin.close();
    outputFile.close();
    return 0;
}

int loadData(ifstream& fin, string rideNames[], double q1Earnings[], double q2Earnings[],
             double q3Earnings[], double q4Earnings[])
{
    int i = 0;
    double cost = 0;
    double earnings;
    getline(fin, rideNames[i]);
    while (fin)
    {
        fin >> cost;
        fin >> earnings;
        q1Earnings[i] = earnings - cost;
        fin >> earnings;
        q2Earnings[i] = earnings - cost;
        fin >> earnings;
        q3Earnings[i] = earnings - cost;
        fin >> earnings;
        q4Earnings[i] = earnings - cost;
        fin.get();
        i++;
        getline(fin, rideNames[i]);
    }
    return i;
}

double calculateQuarterlyEarnings(int rideCount, const double earnings[])
{
    double totalQuarterlyEarnings = 0;
    for (int i = 0; i < rideCount; i++)
    {
        totalQuarterlyEarnings += earnings[i];
    }
    return totalQuarterlyEarnings;
}

int profitable(int rideCount, const double earnings[])
{
    int quarterlyProfitCounter = 0;
    for (int i = 0; i < rideCount; i++)
    {
        if (earnings[i] > 0)
        {
            quarterlyProfitCounter++;
        }
    }
    return quarterlyProfitCounter;
}

void calculateYearlyProfits(int rideCount, const double q1Earnings[], const double q2Earnings[],
                            const double q3Earnings[], const double q4Earnings[], double yearly[])
{
    for (int i = 0; i < rideCount; i++)
    {
        yearly[i] = q1Earnings[i] + q2Earnings[i] + q3Earnings[i] + q4Earnings[i];

        cout << setw(95) << right << yearly[i] << endl;
    }
}

void generateReport(int rideCount, const string rides[], const double q1Earnings[],
                    const double q2Earnings[], const double q3Earnings[], const double q4Earnings[],
                    const double yearly[])
{

}
Hello aardalde,

By putting the green check on your first post you are telling everying that you have solved your problem and do not need any help.


"You'll need to print out the yearly profits in generateReport() along with everything else (that's the only way to get it to appear all together)

- so make sure you run calculateYearlyProfits() before calling generateReport() (then it'll have the data ready to print!)"


This says that everything in "main" the prints the report needs to be in the "generateReport()" function.

This will take a little while to figure out what is needed to be moved before it can be tested.

Again I ask that you post the input file, so everyone can use the same information.

Andy
Hello aardalde,

I managed to move everything around and figure out the problems.

Line 26 creates the variable "rideCount" It would be nice if it was initialized to (0) zero, but not matter it is never used in "main"

On line 45 you create the variable "rides" that you do use, but you should use "rideCount".

On line 35 you started with fin.open("report.txt"); // <--- How do you know that the file opened? . The answer is you do not because you never check it.

When I was finished it produces the report:

What file should be opened for processing? report.txt


           RIDE NAME    Q1 Earnings    Q2 Earnings    Q3 Earnings    Q4 Earnings         YEARLY
***********************************************************************************************
              Ride 1          25.00         125.00         225.00         325.00         700.00
              Ride 2         100.00         200.00         300.00         400.00        1000.00
***********************************************************************************************
            TOTALS:          125.00         325.00         525.00         725.00        1700.00
  Profitable Rides:             2/2            2/2            2/2            2/2


I did have to add in the for loop for printing the yearly totals. Where you are printing them in the "calculateYearlyProfits" function is the wrong place.

Andy
Topic archived. No new replies allowed.