Trouble pulling data from LOOP

I just need some help pulling the data from my loop. So lets say there was 10 transactions from my program before it was ended, I then need to total # units, average, total costs... etc. but I can't seem to pull out the correct data (except for total # of transactions). Thank you for any/all help.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

	double p; // price
	int u; // units
	int t = 1; // transactions

	int totalT, totalU; // total Transactions & Units
	double aveU, aveR, larg, small, totalR; // average Units & Revenue, Largest/Smallest Transaction amount, total Revenue

	cout << setprecision(2) << fixed;

	cout << "Enter transactions (# units / @ price)" << endl;
	cout << "Type -99 & 0 to stop" << endl;

	do
	{		
		cin >> u >> p;
		cout << "Transaction #" << " " << t++ <<":" << " " << u << " " << p << endl;

	} while (u != -99 && p != 0);

	
	totalT = t-2;
	

	cout << "TRANSACTION PROCESSING REPORT" << endl;
	cout << "Transactions Processed:" << "\t" << totalT << endl;
	cout << "Units Sold:" << "\t" << endl;
	cout << "Average Units per Order:" << "\t" << endl;
	cout << "Largest Transaction:" << "\t" << endl;
	cout << "Smallest Transaction:" << "\t"  << endl;
	cout << "Total Revenue:" << "\t"  << endl;
	cout << "Average Revenue:" << "\t" << endl;


	return 0;
}
You're not updating variables like totalU and totalR in the loop.
And most of your couts at the end are not printing any variable at all.
Here's an example of the kind of thing you should do:
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
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int totalT = 0, totalU = 0;
    double totalR = 0;

    cout << "Enter transactions: units price\n";
    cout << "Enter 0 to stop\n";

    while (true) {
        int u;
        cin >> u;
        if (u <= 0)
            break;
        double p;
        cin >> p;
        totalT++;
        totalU += u;
        totalR += u * p;
    }

    cout << setprecision(2) << fixed;
    cout << "totalT: " << totalT << '\n';
    cout << "totalU: " << totalU << '\n';
    cout << "totalR: " << totalR << '\n';
}

Thank you for the response. The assignment I have requires the user to enter -99 0 to end the program. The last question I have is, how to not add the last transaction to the totals? This setup counts the final transaction
why would you not want to add the last transaction -- isn't that something the user enters legitimately? User either enters a transaction, or enters "0" or so, in which case the loop dies without anything getting incremented, so I'm not seeing your issue.
Change the loop to a while(true) loop and break out when you detect -99 and 0. Then you can update the totals. Something like:
1
2
3
4
5
6
7
8
9
10
while(true) {		
	cin >> u >> p;
	if (u == -99 && p == 0) {
		break;
	}
	cout << "Transaction #" << " " << t++ <<":" << " " << u << " " << p << endl;
	totalT++;
	totalU += u;
	totalR += u*p;
}
One last issue. Thank you all for replying, I have taken the information and rewrote some of the code and I have everything solved except for getting the smallest transaction. I can't seem to figure out what is wrong, the largest is working but smallest is not.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

	double p; // price
	int u; // units
	int t = 1; // transactions

	int totalT = 0;// total Transactions
	double aveU, aveR, large = 0, small = 0, totalR = 0, totalU = 0;  // average Units & Revenue, Largest/Smallest Transaction amount, total Revenue & Units

	cout << setprecision(2) << fixed;

	cout << "Enter transactions (# units / @ price)" << endl;
	cout << "Type -99 & 0 to stop" << endl;
	cout << endl;

	while (true) 
	{
		cin >> u >> p;
		if (u == -99 && p == 0) // runs loop until user enters -99 & 0
			break;
		cout << "Transaction #" << " " << t++ <<":" << " " << u << " " << p << endl;	

		totalU+= u; // total units
		totalT++; // total transactions
		totalR += u*p; // total revenue
		aveR = totalR/totalT; // average revenue
		aveU = totalU/totalT; // average units

		if (u*p > large)
			large = u*p; // largest transaction
		if (u*p < small)
			small = u*p; // smallest transaction
	}	
	cout << endl;
	
	cout << "----------TRANSACTION PROCESSING REPORT------------" << endl;
	cout <<"Transactions Processed:" << "\t" <<  "\t" << setw(7) << totalT << endl;
	cout <<"Units Sold:" << "\t" <<  "\t" << "\t"<< setw(7) << totalU << endl;
	cout << "Average Units per Order:" << "\t" << setw(7)  << aveU << endl;
	cout << "Largest Transaction:" << "\t""\t" << setw(7) << large << endl;
	cout << "Smallest Transaction:" << "\t" << "\t" << setw(7) << small << endl;
	cout << "Total Revenue:" << "\t" << "\t"<< "\t" << setw(7) << totalR << endl;
	cout <<"Average Revenue:" << "\t" << "\t" << setw(7) << aveR << endl;

	return 0;
}


Any ideas? Thanks again to all who have responded.
Last edited on
I'm not near my computer to do the testing for this but if I place u*p into a variable, would that work for large/small? It still seems odd that large totaling out just fine but small is not.
Topic archived. No new replies allowed.