|
-
May 31st, 2012, 12:19 PM
#1
Total not adding correctly
Hey all
I've written a program that is designed to add records which include, itemId, itemPrice, discount, quantity. It then adds the Item prices of all the records together (Well that's what it should be doing), but the problem i'm experiencing is that i'm not getting the total of all records. Any help would be appreciated.
Code:
#include <iostream>
using namespace std;
int readSaleRecord(unsigned&, double&, char&, unsigned&, double&);
double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity);
void displayTotalCost(double totalCost, unsigned recordNum, bool aborted);
int main()
{
unsigned itemId, quantity;
double itemPrice, totalCost, recordNum= 0, discount;
char discountType;
bool aborted=false;
readSaleRecord(itemId, itemPrice, discountType, quantity, recordNum);
calculateItemCost(itemPrice, totalCost, discountType, quantity);
displayTotalCost(totalCost, recordNum, aborted);
system("pause");
return 0;
}
void displayTotalCost(double totalCost, unsigned recordNum, bool aborted)
{
cout << "The total cost is $" <<totalCost<<"\n";
cout << "from " <<recordNum<<" record(s) \n";
return;
}
int readSaleRecord(unsigned & itemId, double & itemPrice,char & discountType, unsigned & quantity, double& recordNum)
{
int anotherSale = 0;
cout << "Enter item ID: ";
cin >> itemId;
if (itemId==0 || itemId >1000000)
{
cout << "incorrect id";
system("pause");
exit(0);
}
cout << "Enter full item price: ";
cin >> itemPrice;
if (itemPrice<0)
{
cout << "please enter postive integer";
system("pause");
exit(0);
}
cout << "Enter discount type: ";
cin >> discountType;
if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
{
cout << "invalid entry";
system("pause");
exit(0);
}
cout << "Enter quantity: ";
cin >> quantity;
if (quantity<0)
{
cout << "please enter postive integer";
system("pause");
exit(0);
}
cout << "would you like to enter another sale? (1 for yes and 0 for no)";
cin >> anotherSale;
recordNum++;
while(anotherSale==1)
{
cout << "Enter item ID: ";
cin >> itemId;
if (itemId==0 || itemId >1000000)
{
system("pause");
exit(0);
}
cout << "Enter full item price: ";
cin >> itemPrice;
if (itemPrice<0)
{
cout << "please enter postive integer";
system("pause");
exit(0);
}
cout << "Enter discount type: ";
cin >> discountType;
if (discountType!='N' && discountType!='D' && discountType!='T' && discountType!='B')
{
cout << "invalid entry";
system("pause");
exit(0);
}
cout << "Enter quantity: ";
cin >> quantity;
if (quantity<0)
{
cout << "please enter postive integer";
system("pause");
exit(0);
}
cout << "Enter another record? (1 for yes and 0 for no)";
cin >> anotherSale;
recordNum++;
}
return (itemId, itemPrice, discountType, quantity, recordNum);
}
double calculateItemCost(double itemPrice, double& totalCost, char discountType, unsigned quantity){
double discount;
switch (discountType){
case 'N': discount= 1; break;
case 'B': discount = 0.9; break;
case 'D': discount = 0.8; break;
case 'T': discount = 0.7; break;
default: discount =1;
}
totalCost=itemPrice*discount*quantity;
return totalCost;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|