why does my answer get a lot of decimals
my answer is example 1.000000 why is it so ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int invno, quantity;
double up, tp;
cout << "Please enter the invoice No, Quantity and Unit Price:" ;
cin >> invno >> quantity >> up ;
tp = quantity * up;
cout << left << fixed;
cout << setw(15) << "Invoice No" << setw(15) << "Quantity" << setw(15) << "Unit Price" << setw(15) << "Total Price"<< endl;
cout << setw(15) << invno << setw(15) << quantity << setw(15) << up << setw(15) << tp << endl;
system ("pause");
return 0;
}
.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <iomanip>
using namespace std ;
int main ()
{
int invno, quantity;
double unit_price ; //, tp;
std::cout << "Please enter the invoice No, Quantity and Unit Price:" ;
std::cin >> invno >> quantity >> unit_price ;
const double total_price = quantity * unit_price ;
// http://www.cplusplus.com/reference/iomanip/setprecision/
cout << left << fixed << setprecision(2) // (fixed) with two digits after the decimal
<< setw(15) << "Invoice No" << setw(15) << "Quantity"
<< setw(15) << "Unit Price" << setw(15) << "Total Price\n"
<< setw(15) << invno << setw(15) << quantity
<< setw(15) << unit_price << setw(15) << total_price << '\n' ;
}
|
Topic archived. No new replies allowed.