Results coming up as 0

I'm a beginning student learning about loops. My assignment is to write a program that will calculate the total amount of money for book sales at an online store and to provide the subtotal, tax, discount, shipping and total. When I ran my program through g++ there were no errors. However, when I run it, it displays the subtotal, and tax as 0, as well as displaying the total incorrectly. If anyone could provide me with any guidance as to how to fix this error, it would be greatly appreciated! :)

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
66
67
68
69
70
71
72
73
74
75
76
77

// This program will calculate the total amount of money for book sales at an online store

#include <iostream>
# include <iomanip>
using namespace std;

int main ()
{

// Get the number of books, price for each book and calculate the subtotal
  int number, counter, subtotal = 0;
  double price;
  cout << "Enter the number of books in the sale:";
  cin >> number;

  for (counter = 1; counter <= number; counter++)
    {
    cout << "Enter the price:";
    cin >> price;
    price+=subtotal;
    }

// Get the shipping method
  char shipping;
  double shipping_price;
  cout << "Enter the shipping method [S] Standard shipping [E]Expedited shipping:";
  cin >> shipping;
  shipping_price;
  if (shipping == 'S')
    {
      shipping_price = 4.99;
    }
  else
    {
      shipping_price = 12.99;
    }

// Display the subtotal
  cout << fixed << showpoint << setprecision (2);
  cout << "Subtotal:" << subtotal << endl;

// Calculate and display tax
  double tax;
  cout << fixed << showpoint << setprecision (2);
  tax = subtotal * .05;
  cout << "Tax:" << tax << endl;

// Calculate and display discount
  double discount;
  if (subtotal < 50)
    {
    discount = 0.00;
    cout << "Discount:" << discount << endl;
    }
  else if (subtotal >= 50 && subtotal <= 100)
    {
    discount = subtotal * .10;
    cout << "Discount:" << discount << endl;
    }
  else
    {
    discount = subtotal * .15;
    cout << "Discount:" << discount << endl;
    }

// Display shipping
  cout << "Shipping:" << shipping_price << endl;

//Display total
  double total;
  cout << fixed << showpoint << setprecision (2);
  total = subtotal + tax - discount + shipping_price;
  cout << "Total:" << total << endl;

  return 0;
}


These are the results when I run the program:
> g++ bookSales_LindsayLewis.cpp
> ./a.out
Enter the number of books in the sale:5
Enter the price:2.99
Enter the price:12.45
Enter the price:13.23
Enter the price:21.99
Enter the price:24.59
Enter the shipping method [S] Standard shipping [E]Expedited shipping:S
Subtotal:0
Tax:0.00
Discount:0.00
Shipping:4.99
Total:4.99
price+=subtotal; is probably your problem (this adds subtotal to price and no the other way around).
The problem is in your for loop, because it is a simple error I will give you the answer. In line 21 you have it as "price += subtotal". This means that price will always be 0 and then will add 0 each loop. Switch it to "subtotal += price" and the problem is fixed. Also make sure to declare subtotal as a double, not an int. I believe everything should work now.
Last edited on
Works perfectly! :) Thank you so much for your help!!
Topic archived. No new replies allowed.