Moving functions & main() code to void generateReport()

"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 just don't know how to move the needed code into generateReport(). 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()
{
    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 << setw(13) << right
         << profitable(rides, yearly) << '/' << 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[])
{

}
Have you studied structures and classes yet?

I just don't know how to move the needed code into generateReport().


What have you tried?

Have you tried to just cut and paste all the cout statements into the function?

By the way none of the other functions should have any cout statements.

Since you're using arrays how are you insuring that your data entry loop doesn't overflow the bounds of your arrays?




The basic instruction is to turn your main() function into generateReport(), and write a new main() function that calls generateReport().

[edit] Not all of main(), of course. Only the stuff that produces the report, lines 37–70.
Last edited on
Topic archived. No new replies allowed.