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.
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?
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.
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.
#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
doubleconst 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
doubleconst 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';
}