-
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. :confused:
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;
}
-
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.
-
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