variable initialization fail

//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
}

cout << fixed << setprecision(2);
cout << "\nUnits Sold:" << setw(49) << numSold <<endl;
cout << "Purchase Price per Unit:" << setw(26) << "$" << setw(10) << pPrice << endl;
cout << "Balance Due:" << setw(38) << "$" << setw (10) << totalDue << endl;
return 0;
}

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.

Thanks.
What is the message you are getting when it crashes?
"Debug Error!

Program: ...studio
2010\Projects\Software_Sales\Debug\Software_Sales.exe
Module: ...studio
2010\Projects\Software_Sales\Debug\Software_Sales.exe
File:

Run-Time Check Failure #3 - The variable 'pPrice' is being used without being initialized.

(Please retry to debug the application)

[Abort] [Retry] [Ignore]"

Put
1
2
3
4
5
6

if (flag)
{
cout << "Invalid entry, run the program again.\n";
return 0; //ends the program prematurely if flag is true
}


before totalDue = numSold * pPrice;
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 :)
Topic archived. No new replies allowed.