Total Calculations not right

Hi i am making a program that can calculate the total of two items including tax. I am stuck on the actual calculations of the program, the values it displays for item1total and item2total is wrong, but the calculations for it seems ok?. Can i get some insight of what i did wrong?

Here is a sample, the prices display 0, which i think is something wrong with my formulas, but they seem okay to me.

Enter the name of item 1:Apple
Enter number of Apple and the price of each:2 2.1
Enter the name of item 2:Grapes
Enter number of Grapes and the price of each:3 3.1
Item Count Price
==== ==== ====
Apple 2.00 0.00
Grapes 3.00 0.00
Tax 0.00
Total 0.00

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
  #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const double tax_rate = 0.875;

    string item1;
    string item2;
    double item1quantity;
    double item2quantity;
    double item1price;
    double item2price;
    double item1total;
    double item2total;
    double Subtotal;
    double Tax;
    double Total;

    item1total = (item1price * item1quantity);
    item2total = (item2price * item2quantity);
    Subtotal = (item1total + item2total);
    Tax = (Subtotal * tax_rate);
    Total = (Subtotal + Tax);

    cout << setprecision(2) << fixed;
    cout << "Enter the name of item 1:";
    cin >> item1;
    cout << "Enter number of "<< item1 << " and the price of each:";
    cin >> item1quantity >> item1price;

    cout << "Enter the name of item 2:";
    cin >> item2;
    cout << "Enter number of "<< item2 << " and the price of each:";
    cin >> item2quantity >> item2price;

    cout << setw(10) << "Item" << setw(20) << "Count" << setw(20) << "Price" << endl;
    cout << setw(10) << "====" << setw(20) << "====" << setw(20) << "====" << endl;
    cout << setw(10) << item1 << setw(20) << item1quantity << setw(20) << item1total << endl;
    cout << setw(10) << item2 << setw(20) << item2quantity << setw(20) << item2total << endl;
    cout << setw(10) << "Tax" << setw(40) << Tax << endl;
    cout << setw(10) << "Total" << setw(40) << Total << endl;
    return 0;
}
Last edited on
Well, think of it this way: the code here is going sequentially, in regards to placement. Take this statement:

item1total = (item1price * item1quantity);

What is the current value of item1price and item1quantity when you assign item1total that value?
Thank you Ispil, i figured it out after you said that the code was going sequentially. I moved the calculations after the "cin" input and it works great! Would you believe that i spent 30 minutes trying to get it work? Lifesaver thanks again, lesson learned. :)
Topic archived. No new replies allowed.