Thread: Program output not right

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    15

    Program output not right

    I need some help. My output is all wrong. I am trying to get output of cost of books purchased (will be different amounts according to category), sales tax due, total due, category and number of books purchased. I have only been doing C++ for about a month now and I am very green to say the least.

    Code:
    
    # include <iostream>
    # include <iomanip>
    # include <cmath>
    
    using namespace std;
    
    char old;
    char new;
    char temporary;
    
    
    int main()
     {
             //declare variables
    	    char category;
    	    double costOfBooksPurchased;
    	    double salesTaxDue;
    	    double totalDue;
    	    int number_of_books_purchased;
                        double oldBookPrice;
                        double newBookPrice;
                        double temporaryBookPrice;
    	    double bookPrice;
    
    
      cout << fixed << showpoint << setprecision (2);
    
      cout << "Enter the number of books purchased: ";
      cin >> number_of_books_purchased;
      cout << endl;
    
      cout << "Enter the category: old, new, temporary: ";
      cin >> category;
      cout << endl;
    
      number_of_books_purchased = number_of_books_purchased;
    
      category = old, new, temporary;
    
    
      if (number_of_books_purchased >=6)
           {
                 temporaryBookPrice = number_of_books_purchased * 50;
                 newBookPrice = number_of_books_purchased * 35;
                 oldBookPrice = number_of_books_purchased * 20;
            }
      else 
           {
                temporaryBookPrice = number_of_books_purchased * 45;
                newBookPrice = number_of_books_purchased * 30;
                oldBookPrice = number_of_books_purchased * 15;
            }
    
            
            bookPrice = temporaryBookPrice;
            bookPrice = newBookPrice;
            bookPrice = oldBookPrice;
    
            costOfBooksPurchased = number_of_books_purchased *
                                                      bookPrice;
    
            salesTaxDue = costOfBooksPurchased * .04;
    
            totalDue = costOfBooksPurchased +  salesTaxDue;
    
      cout << "The number_of_books_purchased = " <<
                    number_of_books_purchased << endl;
    
      cout << "The category = " << category << endl; 
    
      cout << "The costOfBooksPurchased = $" <<   
                   costOfBooksPurchased << endl;
    
      cout << "The salesTaxDue = $" << salesTaxDue << endl;
    
      cout << "The totalDue = $" << totalDue << endl;
            
      return 0;
     }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Ok, where to start;

    This line is not needed:
    Code:
    number_of_books_purchased = number_of_books_purchased;
    This line is not valid:
    Code:
    category = old, new, temporary;
    You need to reread up on char's


    These lines:
    Code:
            bookPrice = temporaryBookPrice;
            bookPrice = newBookPrice;
            bookPrice = oldBookPrice;
    The value of bookPrice will only end up holding the value of oldBookPrice.


    This line:
    Code:
    costOfBooksPurchased = number_of_books_purchased *
                                                      bookPrice;
    Is actually faulty becuse in your If-Else statements you already multiplied the number of books purchased times the price. So you are now multiplying number_of_books_purchased by the bookPrice, what was already previously the same calculation.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You cannot use the keyword new as a variable name.

    initialize your variables.

    You can not read a string into a single char.

    You only ask how many books one time, yet your calculations imply there was more than one entry.
    Maybe you need multiple questions , or some sort of loop. I'm not sure exactly what you're trying to do.

    Code:
    cout << "Enter the number of old books purchased:";
    cin >> num_old_books;
    cout << "Enter the number of new books purchased:";
    cin >> num_new_books;
    ......
    Then display results for each category as well as the totals
    Last edited by spydoor; 09-16-2005 at 01:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 12:49 AM
  2. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  3. Unusual program Output, Help
    By capvirgo in forum C Programming
    Replies: 8
    Last Post: 02-06-2008, 03:13 AM
  4. Redirecting program output straight to an edit control
    By bennyandthejets in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2004, 08:25 AM
  5. Program Output
    By lavon in forum C Programming
    Replies: 1
    Last Post: 03-19-2002, 10:32 PM