Hello, I was wanting some tips on how I can go about moving the decimal point of the answer to weight, to show exact amount of change. I have tried setprecision, but could not get it to compile.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double distance, weight;
float a, x, y, z;
a = 1.10;
x = 2.20;
y = 3.70;
z = 4.80;
cout << "Freight Shipping Rates\n";
cout << "--------------------------------------------------------\n";
cout << "5 pounds or less"; cout.width(39); cout << "$1.10\n";
cout << "Over 5 pounds but not more than 15 "; cout.width(20); cout << "$2.20\n";
cout << "over 15 pounds but not more than 30"; cout.width(20); cout << "$3.70\n";
cout << "over 30 pounds but not more than 50"; cout.width(20); cout << "$4.80\n";
cout << "-----------------------------------------------------------------\n";
cout << "NOTICE: We do not ship packages over 50 pounds\n";
cout << "We do not ship less than 10 miles or more than 3,000 miles\n";
cout << "Please enter the weight in pounds of your package:";
cin >> weight;
cin.ignore();
cout << "Please enter the distance in miles of your shipment";
cin >> distance;
cin.ignore();
if (weight <= 5) { weight *= a; }
elseif (weight > 5 && weight <= 15) { weight *= x; }
elseif (weight > 15 && weight <= 30) { weight *= y;}
elseif (weight > 30 && weight < 50) { weight *= z; }
else"We do not ship packages over 50 pounds.";
cout << "$" << weight;
system("PAUSE");
return 0;
}
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
double distance, weight;
float a, x, y, z;
a = 1.10;
x = 2.20;
y = 3.70;
z = 4.80;
cout << "Freight Shipping Rates\n";
cout << "--------------------------------------------------------\n";
cout << "5 pounds or less"; cout.width(39); cout << "$1.10\n";
cout << "Over 5 pounds but not more than 15 "; cout.width(20); cout << "$2.20\n";
cout << "over 15 pounds but not more than 30"; cout.width(20); cout << "$3.70\n";
cout << "over 30 pounds but not more than 50"; cout.width(20); cout << "$4.80\n";
cout << "-----------------------------------------------------------------\n";
cout << "NOTICE: We do not ship packages over 50 pounds\n";
cout << "We do not ship less than 10 miles or more than 3,000 miles\n";
cout << "Please enter the weight in pounds of your package:";
cin >> weight;
cin.ignore();
cout << "Please enter the distance in miles of your shipment";
cin >> distance;
cin.ignore();
if (weight <= 5) { weight *= a; }
elseif (weight > 5 && weight <= 15) { weight *= x; }
elseif (weight > 15 && weight <= 30) { weight *= y;}
elseif (weight > 30 && weight < 50) { weight *= z; }
else"We do not ship packages over 50 pounds.";
cout << setprecision(3) "$" << weight;
system("PAUSE");
return 0;
}
Actually I would move line 55 to line 22 to set the output before you use it. Once the line is executed it affects every other call to "cout". I kind of like this version when I use it: std::cout << std::fixed << std::showpoint << std::setpercision(2). The "showpoint" will print xx.00 when there is nothing to the right of the decimal.
Hope that helps,
Andy
Edit: My apologies I was thinking to far ahead when I said move line 55 to 22. I missed the part about printing weight. Printing weight will need to stay at line 55. The code I mentioned will work at line 22.