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!
Code:
// 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