8. Total Purchase

Hello, this is tetricsteel. I am posting this forum to see if anyone can help me
in reguards of this program I have ran and successfully executed. However I want to make a couple of adjustments to the details of the program. By the way, this
program was created in Visual Studio C++. The details I want to cover concern the value of the total tax and the net worth price after tax. This program tells me :

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
60
61
62
63
64
# include <iostream> //This is regula input output stream.
# include <iomanip> // I am calling a manipulator in order to control the amount of decimal places I want my answer to have. 
# include <string> // For the If statement and the else statement input. 

using namespace std;

int main()
{ // I open up my code 

double Price_of_item_1 = 12.95,Price_of_item_2 = 24.95,Price_of_item_3 = 6.95,Price_of_item_4 = 14.95,Price_of_item_5 = 3.95;
// Knotest the fact that the variables are not just being initilized but they are being called with onlly one line. By the use of " , " commas. 

cout<<"Hello sir, I am here to bring you the check. "<< "\n" "\n"; // Talking to the user about the payment.
cout<< "You are looking at the check and see what it says.....\n \n"; // This is as it is, just you looking at the check. 
cout<< " Orcha Restaurant-Bringing you the most delicious gormet Indian food :) \n \n"; // Name of the restaurant. 

cout<< "$ "<<Price_of_item_1<< "\n" << "$ "<<Price_of_item_2<< "\n" << "$ "<<Price_of_item_3<< "\n" 
<< "$ "<<Price_of_item_4<< "\n"<< "$ "<< Price_of_item_5<< " + "<< endl<< "_________"<< "\n"<< "Subtotal : "<<
Price_of_item_1+Price_of_item_2+Price_of_item_3+Price_of_item_4+Price_of_item_5<<endl;
// All of this was squashed together to preserve space, however simplicity was not considered much. 
// Knowtest also that I am putting the $ signs and the "\n" before and after the variable is called into play. 
// Also knowtest the line "____________". This is the line that comes before the subtotal. 
// ALso knotwest that the summation was not done until the end of the object execution. 
// ALso do really pay attention to how every inch of the code is spaced. Especially the physical code.

double total_p = Price_of_item_1+Price_of_item_2+Price_of_item_3+Price_of_item_4+Price_of_item_5; 
/* This variable was made in order to make the program simpler, cause I could have just let the program to equal the amount like 
 total_p is equal or ----total_p == $ 63.35, however programs are about declaring variables so that it knows what you are talking about.*/

cout<<"\n"<< "+ Tax (6% of networth) : "<<fixed<< setprecision(2)<< (total_p*.06)<<endl;
/* Knowtest the use of the "fixed" and "setprecision" manipulators. Also the (2) in the setprecision. That indicates that I want 2 decimal places 
after the original decimal point in my final calculation. */

double tax_p = total_p*.06;
/* Last, but not least the last declared vaiable. Knowtest the fact that its a double digit. If it were not double like an int I could not have used 
the .06 or 6% in the calculation because otherwise the number .06 would have been truncated into the whole 0. */

cout<<"\n"<< "Your grand total is: ---> "<<tax_p + total_p<<endl; 
cout<<"********************************************************\n";
cout<<" How will you choose to pay sir? With debit or cash?  \n"; 
cout<<"*********************************************************\n"<<endl;

cout << "Input your payment of choice------debit or cash-----.\n"; 
string debit_or_cash; 
getline (cin, debit_or_cash, '\n') ; 

if ( debit_or_cash == "debit" )
{
cout<< "Sorry bro, we dont even have a debit machine... Ma bad \n \n" ;
}

if ( debit_or_cash == "cash" )
{
cout<< " Oh! well if its cash I'll take that Thank You Very Much.\n "; 
}
else 
{
	cout<< "Sorrry you have input an incorrect answer.\n"; 
}

system("pause"); 

} // I close my code and finish. 
 



I did include an If funtion there and it gave me errors, however its the total
sig figs I get in the subtax and the total price. I want three sigfigs in the price (e.g 14.01*.06= 2.33 not 2.332) I just want to solve that tiny problem. Thank You everyone for your help in advanced.
Last edited on
#include <iomanip> and use the setprecision() manipulator.

cout << fixed << setprecision( 2 ) << (tax_p + total_p) << endl;

Hope this helps.
Hello Duoas :

Thank you for your help on the problem. It worked! and it have me a great answer. However the answer is off by one cent. The answer should have been 67.57, not 67.58. I know that probably the thing to do was add manipulators, however the answer is mainly wrong when the Tax multiplication is performed. You see the tax is originally .06 and it multiplies times a 4 digit number. It comes out by manipulation a 2 decimal number when added but the multiplication gives off by one small cent. Thank You for the help again, but if you can help me in this other one it would be greatly appreciated. BTW here is the new code I wrote that will make more sense.


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
60
61
62
63
64
65
# include <iostream> //This is regula input output stream.
# include <iomanip> // I am calling a manipulator in order to control the amount of decimal places I want my answer to have. 
# include <string> // For the If statement and the else statement input. 

using namespace std;

int main()
{ // I open up my code 

double Price_of_item_1 = 12.95,Price_of_item_2 = 24.95,Price_of_item_3 = 6.95,Price_of_item_4 = 14.95,Price_of_item_5 = 3.95;
// Knotest the fact that the variables are not just being initilized but they are being called with onlly one line. By the use of " , " commas. 

cout<<"Hello sir, I am here to bring you the check. "<< "\n" "\n"; // Talking to the user about the payment.
cout<< "You are looking at the check and see what it says.....\n \n"; // This is as it is, just you looking at the check. 
cout<< " Orcha Restaurant-Bringing you the most delicious gormet Indian food :) \n \n"; // Name of the restaurant. 

cout<< "$ "<<Price_of_item_1<< "\n" << "$ "<<Price_of_item_2<< "\n" << "$ "<<Price_of_item_3<< "\n" 
<< "$ "<<Price_of_item_4<< "\n"<< "$ "<< Price_of_item_5<< " + "<< endl<< "_________"<< "\n"<< "Subtotal : "<<
Price_of_item_1+Price_of_item_2+Price_of_item_3+Price_of_item_4+Price_of_item_5<<endl;
// All of this was squashed together to preserve space, however simplicity was not considered much. 
// Knowtest also that I am putting the $ signs and the "\n" before and after the variable is called into play. 
// Also knowtest the line "____________". This is the line that comes before the subtotal. 
// ALso knotwest that the summation was not done until the end of the object execution. 
// ALso do really pay attention to how every inch of the code is spaced. Especially the physical code.

double total_p = Price_of_item_1+Price_of_item_2+Price_of_item_3+Price_of_item_4+Price_of_item_5; 
/* This variable was made in order to make the program simpler, cause I could have just let the program to equal the amount like 
 total_p is equal or ----total_p == $ 63.35, however programs are about declaring variables so that it knows what you are talking about.*/

cout<<"\n"<< "+ Tax (6% of networth) : "<<fixed<< setprecision(2)<< (total_p*.06)<<endl;
/* Knowtest the use of the "fixed" and "setprecision" manipulators. Also the (2) in the setprecision. That indicates that I want 2 decimal places 
after the original decimal point in my final calculation. */

double tax_p = total_p*.06;
/* Last, but not least the last declared vaiable. Knowtest the fact that its a double digit. If it were not double like an int I could not have used 
the .06 or 6% in the calculation because otherwise the number .06 would have been truncated into the whole 0. */

cout<<"\n"<< "Your grand total is: ---> "<<tax_p + total_p<<endl; 
cout<<"********************************************************\n";
cout<<" How will you choose to pay sir? With debit or cash?  \n"; 
cout<<"*********************************************************\n"<<endl;

cout << "Input your payment of choice------debit or cash-----.\n"; 
string debit_or_cash; 
getline (cin, debit_or_cash, '\n') ; 

if ( debit_or_cash == "debit" )
{
cout<< "Sorry bro, we dont even have a debit machine... Ma bad \n \n" ;
}

if ( debit_or_cash == "cash" )
{
cout<< " Oh! well if its cash I'll take that Thank You Very Much.\n "; 
}
else 
{
	cout<< "Sorrry you have input an incorrect answer.\n"; 
}

system("pause"); 

} // I close my code and finish. 





Thanx again for the help.
Last edited on
The output rounds the last digit. So if you have 67.57639 it will get printed as "67.58".

This is actually an issue with the math in your program and not with the fact that the float-to-string conversion is doing rounding.


It is actually a bad idea to use floats for monetary calculations. You might be better off using just integers, and putting the decimal point in the right spot when you print output. (This is called "fixed point". You can google around that.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

template <typename Integer = long int, size_t Scale = 10000>
struct fixed_point_t
  {
  typedef Integer type;
  Integer& integer;
  fixed_point_t( Integer& integer ): integer( integer ) { }
  };

template <typename Integer, size_t Scale>
std::istream& operator >> ( std::istream& ins, const fixed_point_t <Integer, Scale> & integer )
  {
  double x;
  ins >> x;
  integer.integer = x * Scale;
  return ins;
  }

template <typename Integer, size_t Scale>
std::ostream& operator << ( std::ostream& outs, const fixed_point_t <Integer, Scale> & integer )
  {
  return outs << ((double)integer.integer / Scale);
  }
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iomanip>
#include <iostream>
using namespace std;

int main()
  {
  typedef fixed_point_t <int, 100> fixed_point;

  fixed_point::type price;

  cout << "Please enter a dollar amount> ";
  cin >> fixed_point( price );

  price *= 1.5;  // Truncation of float-to-int makes 12.5 --> 12.

  cout << "Half again as much: " << fixed << setprecision( 2 ) << fixed_point( price ) << endl;

  return 0;
  }

C:\m\prog\foo> a
Please enter a dollar amount> 3.45
Half again as much: 5.17

C:\m\prog\foo>

Notice that you didn't get a half a cent in the result there? That has nothing to do with the fixed_point manipulator -- it has everything to do with the fact that line 39 there is an integer operation.

    (345 * 1.5) --> 517.5 --> 517

Hope this helps.
Topic archived. No new replies allowed.