Sep 29, 2012 at 4:46pm UTC
Can anyone tell me why this program crashes when I enter 0 for How many items need to be purchased to recieve a discount?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//variables
string itemName;
int numPurchased;
int numneedDiscount;
int numDiscounted;
double priceperItem;
double discountPercent;
double discountDecimal;
double singleDiscount;
double totalDiscount;
double grandtotalnoDisc;
double grandtotalDisc;
string again = "Y";
//constants
const int MIN_PURCHASED = 1;
const int MAX_DISCOUNT = 30;
const int MIN_DISCOUNT = 0;
const double MIN_ITEM_COST = 0;
const int NUM_FOR_DISCOUNT = 0;
while (again == "Y" || again == "y")
{
//read in item name
cout << "\nPlease enter the item name: ";
getline(cin, itemName);
//read in number purchased
cout << "Please enter the number of items purchased: ";
cin >> numPurchased;
while (numPurchased < MIN_PURCHASED)
{
cout << "*** The number of items purchased must\nbe at least 1.";
cout << "\n*** Please reenter: ";
cin >> numPurchased;
}
//read in number that have to be purchased to recieve discount
cout << "How many items need to be\npurchased to receive a discount? ";
cin >> numneedDiscount;
while (numneedDiscount < NUM_FOR_DISCOUNT)
{
cout << "*** The number of items purchased to recieve\n*** a discount must be at least 0.\n*** Please reenter: ";
cin >> numneedDiscount;
}
//read in discount percentage
if (numneedDiscount > NUM_FOR_DISCOUNT)
{
cout << "Please enter discount percentage: ";
cin >> discountPercent;
while (discountPercent < MIN_DISCOUNT)
{
cout << "*** The discount percentage must be\n*** at least 0.00% and\n*** at most 30.00%.\n*** Please reenter: ";
cin >>discountPercent;
}
}
//if no discount percent then set discount percent for single item to 0
else
{
discountPercent = 0;
}
//read in price per item
cout << "Please enter the price of a single item: ";
cin >> priceperItem;
while (priceperItem <= MIN_ITEM_COST)
{
cout << "*** The price of one item must be\n*** greater than $ 0.00\n*** Please reenter: ";
cin >> priceperItem;
}
//calculate items that will be discounted
numDiscounted = numPurchased / numneedDiscount;
//convert percentage discount to decimal
discountDecimal = discountPercent / 100;
//calculate single item discount
singleDiscount = priceperItem * discountDecimal;
}
cout << "" << endl;
return 0;
}
Last edited on Sep 30, 2012 at 2:34pm UTC
Sep 29, 2012 at 11:26pm UTC
That was it! Thanks, should have caught that