Jan 6, 2016 at 8:07pm UTC
How to get the sum number from total column?
For example first loop total=100, sum=100
second loop total=150, sum=250
third loop total=80, sum=330...
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i=1, x=1, id;
float quantity, price, total=0, sum;
string name;
char enter, tick;
cout << "#" << " ID " << "Name" << " Quantity " << "Price" << " Total" << endl;
do {
cin >> id;
cin >> name;
cin >> quantity;
cin >> price;
cout <<"#" << x++ << " " << id << " " << name << " " << quantity << " " << price << " " << quantity*price << endl;
sum=quantity*price;
cout << " " << sum++ << endl;
cin >> tick;
}while (tick == 't' );
return 0;
}
Last edited on Jan 6, 2016 at 8:10pm UTC
Jan 6, 2016 at 10:32pm UTC
maybe add total += sum;
just after line 19.
Jan 6, 2016 at 11:10pm UTC
Your question is not clear.
Are you trying to get the total of all the values on "sum" variable?
l think the answer by Chervil can help in some way.
Reword your question or say what you want the code to do.
Jan 7, 2016 at 9:43am UTC
@Chervil
If i write like you said->
error: invalid operands of types 'float' and '<unresolved overloaded function type>' to binary 'operator<<'|
And how can i add precision in float variables, i tired this ->
<< setprecision(3)<<
it said setprecision was not declared in this scope
tried this ->
<< std::setprecision(3) <<
it said setprecision is not member of 'std'
Last edited on Jan 7, 2016 at 10:26am UTC
Jan 8, 2016 at 11:42am UTC
@Chervil
thanks for
#include <iomanip>
i want to set precision after decimal dot.
found googling
<< fixed
thanks for
1 2
sum = quantity*price;
total += sum;
i learned that i cant put {formulas=variable} in output, but in line yes
Last edited on Jan 8, 2016 at 11:58am UTC