Thread: Assistance in fixing errors

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Assistance in fixing errors

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > price+=subtotal;
    What is being added to what here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assistance with unknown errors?
    By westside222 in forum C++ Programming
    Replies: 9
    Last Post: 11-13-2009, 09:00 PM
  2. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  3. Errors i'm having trouble fixing
    By yaniv89 in forum Windows Programming
    Replies: 5
    Last Post: 08-26-2005, 02:35 PM
  4. How would I got about fixing this?
    By SlyMaelstrom in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2005, 05:34 AM
  5. Fixing errors!
    By Zophixan in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2002, 06:18 PM