SOS: Void Function

How do I get the void function calculateYearlyProfits at the bottom to properly add up the yearly profit for each ride? 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
#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;
    }
    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) << 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[])
    {
        double yearlyProfits = 0;
        for (int i = 0; i < rideCount; i++)
        {
            q1Earnings[i] + q2Earnings[i] + q3Earnings[i] + q4Earnings[i] += yearly[];
        }
    }
yearly[i] = q1Earnings[i] + q2Earnings[i] + q3Earnings[i] + q4Earnings[i];
Hello aardalde,

Without testing it try
yearl[i] += q1Earnings[i] + q2Earnings[i] + q3Earnings[i] + q4Earnings[i]

Not knowing what your input file looks like I can only say this should work. Notice the "i" inside the []s. AS an array you need to tell it which element to use.

Andy
Hello aardalde,

To start with your code is very very hard to read. A blank line here and there will make a big difference. To make my point the function call to "loadData" was hard to see and I missed it and wrote what you already had just to test the function.

My first question is you open the input file on line 33, but how do you know that it is open?

When opening a file stream for input you should always test that it is open and ready to use before you try to use it.

This is what I use. Some of the variable names have changed from what you started with. You can keep them or use what you like.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string rideNames[MAX_STR], inputFileName{ "report.txt" }, outputFileName;  // <--- Changed for testing.

cout << "What file should be opened for processing? " << inputFileName << "\n\n" << endl;
//cout << "What file should be opened for processing? " << endl; // <--- Works better without the "endl".
//cin >> inputFileName;
//fin.open("report.txt");

std::ifstream inFile(inputFileName);

if (!inFile)
{
	std::cout << "\n File " << std::quoted(inputFileName) << " did not open" << std::endl;
	//std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional.
	return 1;  //exit(1);  // If not in "main".
}

Line 3 is a replacement for your lines 4 and 5.

Line 8 is a replacement for line 6.

The if statement is what is most important.

Checking the output stream is not as important as the file will be created, if it does not exist, before it is opened. If you have a path to the file then checking as I did with the input stream is needed.

While I am in "main":
Line 36 is good. You only need to do it once unless you change something somewhere.

Using "right" as you have is unnecessary as the default setting is "right". So unless you use "left" you do not need it with every "setw()".

I also notice that the column heading "Yearly" has nothing under it.

In the "loadData" function I think this would work better for 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
	//getline(fin, rideNames[i]);

	//while (fin)
	while(getline(fin, rideNames[i]))
	{
		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();
		fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		i++;

		//getline(fin, rideNames[i]);
	}

Since I do not have the input file that you are using I created one based on the way the loop reads the file. It is not likely to match what you are using or even have the correct numbers, but it works for now.

The change in your last function has already been mentioned.

I have not looked at the other functions because they appear to work for now.

Andy
Topic archived. No new replies allowed.