Help with the for Loop

I have a problem somewhere with my code, it works and I get it to the point where my output can have multiple transactions until you enter -99 0, but when you are done I am getting 0.00 amounts where there should be totals for the transactions. I posted an example of the out put below my code with just a few transactions. I have been stuck on this issue for days and cannot seem to find what I need to do. Thanks in advance for some 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
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
#include <iostream>
#include <vector>
#include <iomanip> // std::setprecision
using namespace std;

int main()
{
	// Declaring the variables
	int no_of_items, tot_units = 0;
	double unit_price, tot_revenue = 0, avg_revenue;
	int count = 0;

	while (true)
	{
		cout << "Transaction # " << count + 1 << ":";
		cin >> no_of_items;
		cin >> unit_price;
		if (no_of_items == -99)
		{
			break;
		}
		else
		{
			count++;
			tot_units += no_of_items;
			continue;
		}
	}
	// Setting the precision
	std::cout << std::setprecision(2) << std::fixed;

	// Displaying the output
	cout << "\nTRANSACTION PROCESSING REPORT" << endl;
	cout << "Transaction Processed : " << count << endl;
	cout << "Units sold : " << tot_units << endl;
	cout << "Average Units Per Order : " << (double)tot_units / count << endl;

	double max_tran, min_tran;
	max_tran = no_of_items * unit_price;
	min_tran = no_of_items* unit_price;

	// This for loop will find the maximum transaction and minimum transaction
	for (int count = 0; count < unit_price; count++)
	 {
		if (min_tran > no_of_items * unit_price)
			min_tran = no_of_items * unit_price;

		if (max_tran < no_of_items * unit_price)
			max_tran = no_of_items * unit_price;

		tot_revenue += no_of_items * unit_price;
	}

	cout << "Largest Transaction : " << max_tran << endl;
	cout << "Smallest Transaction : " << min_tran << endl;
	cout << "Total Revenue :$ " << tot_revenue << endl;
	avg_revenue = tot_revenue / count;
	cout << "Average Revenue :$ " << avg_revenue << endl;
	return 0;
}

/*
Transaction # 1:23 7
Transaction # 2:43 9
Transaction # 3:54 6
Transaction # 4:23 5
Transaction # 5:56 8
Transaction # 6:-99 0

TRANSACTION PROCESSING REPORT
Transaction Processed : 5
Units sold : 199
Average Units Per Order : 39.80
Largest Transaction : -0.00
Smallest Transaction : -0.00
Total Revenue :$ 0.00
Average Revenue :$ 0.00
Press any key to continue . . .
*/
1
2
3
4
5
6
7
8
for (int count = 0; count < unit_price; count++)
	 {
		if (min_tran > no_of_items * unit_price)
			min_tran = no_of_items * unit_price;

		if (max_tran < no_of_items * unit_price)
			max_tran = no_of_items * unit_price;
          ...


This for loop makes no sense. It's the exact same every time. Nothing changes. Why are you looping but doing exactly the same calculation on every loop?
Last edited on
I am not sure, so what you saying is I don't need a loop, how will I then calculate the largest transactions, smallest, total revenue, and average revenue. I am stuck on this issue. If I would have either 10 or 100 transactions I thought I would need a loop.
You don't store the information entered. There is literally no way to calculate the min and max transactions after all the transactions have been done.

You will have to do it as the data is entered. Each time the user enters data, update the min/max if that new transaction is a new min.max.
Thanks for the advice, can you give me a hint on what I need to get me going in the right direction.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        double max_tran = 0;
         double min_tran =999999999;
	while (true)
	{
		cout << "Transaction # " << count + 1 << ":";
		cin >> no_of_items;
		cin >> unit_price;


    // ADD CODE HERE TO CHECK IF THE TRANSACTION JUST ENTERED IS NEW MAX OR MIN

		if (no_of_items == -99)
		{
			break;
		}
		else
		{
			count++;
			tot_units += no_of_items;
			continue;
		}
	}
I just don't understand, after your help I did some more reading and it looks like to get the min and max of my transactions I need an array, I am new to this, so if I am correct can I get some help with an array.
You don't need an array. Here's an incomplete example to play with:

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

int main() {
    double min_transaction_price = 0.0;
    double max_transaction_price = 0.0;
    
    // working variables for the transaction data
    int quantity = 0; 
    double price = 0.0;
    
    // if the first transaction was read successfully 
    if (std::cin >> quantity >> price) {
        // then the minimum and maximum transaction cost so far is 
        //   the cost of this transaction. 
        
        // compute the transaction price
        double const transaction_price = quantity * price;
        
        min_transaction_price = max_transaction_price = transaction_price;
    } else { // couldn't read any transactions.
        // print an error and quit. 
        std::cerr << "error: no transactions read\n";
        return 1;
    }
    
    // read the remaining transactions:
    while (std::cin >> quantity >> price) {
        // compute the transaction price
        double const transaction_price = quantity * price;
        
        // if the current transaction is valued less than all the others,
        //   keep track of it:
        if (transaction_price < min_transaction_price) 
            min_transaction_price = transaction_price;
        
        // if the current transaction is valued more than all the others,
        //   keep track of it:
        if (transaction_price > max_transaction_price)
            max_transaction_price = transaction_price;
    }
    
    // print the price of the min- and max-valued transactions:
    std::cout << "min-valued transaction: " << min_transaction_price << '\n'
              << "max-valued transaction: " << max_transaction_price << '\n';   
} 
Last edited on
Thanks for your guidance, I figured it out with your help, thanks again!
Topic archived. No new replies allowed.