//Calculate balance due based on software sales.
//A software company sells a package that retails for $99. Quantity discounts are as follows:
//10-19: 20%, 20-49: 30%, 50-99: 40%, 100 or more: 50%.
//Write a program that asks for the number of units purchased and computes the total cost of the purchase.
//Include a flag.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int numSold;
double pPrice, //purchase price per unit
totalDue;
const short PRICE = 99;
bool flag = false;
cout << "How many units sold? ";
cin >> numSold;
if (numSold <= 0)
flag = true;
else if (numSold < 10)
pPrice = numSold * PRICE;
else if (numSold < 20)
pPrice = 0.8 * numSold * PRICE;
else if (numSold < 50)
pPrice = 0.7 * numSold * PRICE;
else if (numSold < 100)
pPrice = 0.6 * numSold * PRICE;
else //further if for 100 or greater not needed
pPrice = 0.5 * numSold * PRICE;
totalDue = numSold * pPrice;
if (flag)
{
cout << "Invalid entry, run the program again.\n";
return 0; //ends the program prematurely if flag is true
}
This program worked fine when I input zero until I put in the flag. Don't know why it's giving me an error on pPrice. Entering zero now causes the program to crash.
Thank you, this works! Can you explain why?
That I can see, the flag is not manipulating pPrice at all.
Would like to be able to not make the same mistake in the future :)