0% found this document useful (0 votes)
7 views7 pages

C++ Shopping Cart with Discounts

The document contains C++ code for a program that calculates order totals and discounts for multiple items. It gets input from the user for item prices, applies discounts for seniors and gift cards, calculates taxes, and outputs a final bill total.

Uploaded by

bob rob
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

C++ Shopping Cart with Discounts

The document contains C++ code for a program that calculates order totals and discounts for multiple items. It gets input from the user for item prices, applies discounts for seniors and gift cards, calculates taxes, and outputs a final bill total.

Uploaded by

bob rob
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

//Functions

int menu();

int items(int);

int age();

int repeat();

int giftCard();

int main() {

//Declaration Block

int itemNo;

bool cont = 1;

bool orderAgain = 1;

bool seniorDiscount = 0;

float gift = 0.00;

float tax = 0.00;

float itemPrice = 0.00;

float totalPrice = 0.00;

float finalPrice = 0.00;

float ageDiscount = 0.00;

float ageDiscountTotal = 0.00;


//Main Loop

while (orderAgain == 1){

cout << "Please enter the price of the item you wish to buy (ex. 5.99): ";

cin >> itemPrice;

seniorDiscount = age();

if(seniorDiscount > 0) {

ageDiscountTotal = ageDiscountTotal + itemPrice;

orderAgain = repeat();

totalPrice = totalPrice + itemPrice;

itemPrice = 0.00;

//Price calculations when done ordering

finalPrice = totalPrice;

ageDiscount = ageDiscountTotal * 0.10;

finalPrice = finalPrice - ageDiscount;

tax = finalPrice * 0.06;

finalPrice = finalPrice + tax;

gift = giftCard();
finalPrice = finalPrice - gift;

//Displays the Bill

cout << " Bill Information\n"

"------------------------------------------------\n"

"Total of all items:" << " $" << setprecision (2) << fixed << totalPrice << endl;

if (ageDiscountTotal > 0) {

cout << "Total senior discounts:" << " -$" << setprecision (2) << fixed
<< ageDiscount << endl;

cout << "Taxes:" << " +$" << setprecision (2) << fixed << tax
<< endl;

if (gift > 0) {

cout << "Gift card amount applied:" << " -$" << setprecision (2) << fixed << gift
<< endl;

cout << "-----\n"

"Bill:" << " $" << setprecision (2) << fixed << finalPrice <<
endl;

system("pause");

return 0;
}

//Asks the user their age to determine if the age discount should be applied

int age()

string age = "";

bool ageDiscount = 0;

int ageLoop = 1;

cout << "Are you 65 years old or older (Y or N)? ";

cin >> age;

for (int i = 0; i < ageLoop; i++) {

if (age == "Y" or age == "y") {

ageDiscount = 1;

} else if (age == "N" or age == "n") {

ageDiscount = 0;

} else {

cout << "Invalid choice. Are you 65 years old or older (Y or N)? ";

cin >> age;

ageLoop = ageLoop + 1;
}

return ageDiscount;

//Asks the user for if they want another item until they say no

int repeat()

string order = "";

bool orderAgain = 0;

int orderLoop = 1;

cout << "Would you like to order another item (Y or N)? ";

cin >> order;

for (int i = 0; i < orderLoop; i++) {

if (order == "Y" or order == "y") {

orderAgain = true;

} else if (order == "N" or order == "n") {

orderAgain = false;

} else {
cout << "Invalid choice. Would you like to order another item (Y or N)? ";

cin >> order;

orderLoop = orderLoop + 1;

return orderAgain;

//Asks the user if they want to apply a giftcard

int giftCard()

string gift = "";

bool giftDiscount = 0;

int giftLoop = 1;

float giftAmount = 0.00;

cout << "Do you have a gift card (Y or N)? ";

cin >> gift;

for (int i = 0; i < giftLoop; i++) {

if (gift == "Y" or gift == "y") {

giftDiscount = true;

cout << "How much money would you like to apply from card? (ex. 5.99): ";
cin >> giftAmount;

} else if (gift == "N" or gift == "n") {

giftDiscount = false;

} else {

cout << "Invalid choice. Do you have a gift card (Y or N)? ";

cin >> gift;

giftLoop = giftLoop + 1;

return giftAmount;

You might also like