decimal move

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.

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
#include <iostream>
#include <iomanip>
using namespace 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; }
						  else if (weight > 5 && weight <= 15)             { weight *= x; }
						  else if (weight > 15 && weight <= 30)            { weight     *= y;}
						  else if (weight > 30 && weight < 50)             { weight *= z; }
						  else "We do not ship packages over 50 pounds.";

						  cout << "$" << weight;

   system("PAUSE");
	return 0;
}
Last edited on
 I have tried setprecision ...

since you don't show what you've actually tried it's difficult to tell but std::setprecision() is indeed the way to go about it:
1
2
3
4
5
6
7
8
9
10
# include <iostream>
# include <iomanip>

int main()
{
   auto decimalNum = 1.23 * 4.56;
   std::cout << std::setprecision(2) << decimalNum << "\n";
   std::cout << std::setprecision(3) << decimalNum << "\n";
   std::cout << std::setprecision(4) << decimalNum << "\n";
}

Notice you're using double's && float's, so you should at least be aware of their differences if not already:
https://stackoverflow.com/questions/2386772/difference-between-float-and-double
Here is the code with setprecision, it still returns answers only one decimal in, no matter what number I enter inside of setprecision.

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
#include <iostream>
#include <iomanip>
using namespace 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; }
						  else if (weight > 5 && weight <= 15)             { weight *= x; }
						  else if (weight > 15 && weight <= 30)            { weight     *= y;}
						  else if (weight > 30 && weight < 50)             { weight *= z; }
						  else "We do not ship packages over 50 pounds.";

						  cout << setprecision(3) "$" << weight;

   system("PAUSE");
	return 0;
}
Line 55: cout << setprecision(3) "$" << weight;
To: cout << fixed << setprecision(3) << "$" << weight << endl;
Hello TheArk,

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.
Last edited on
Awesome it worked, thanks
Topic archived. No new replies allowed.