Anyway, my prof had an assignment to write our own piece of code, which should include the three loops we have learned in class. I decided to create grocery store application in which users can select between regular price, clearance, and special discount items to purchase. It is nearly complete; however I would like to add in a running total using the for loop at the very end, where the menu asks to quit (if you wish to skip ahead, the last default option).
As for loops are my weakest suit currently, I have tried reading tutorials on how to apply the concepts to a running total, but I don't understand. Any help? Thanks in advance.
Also, the code runs fine.
#include <iostream>
#include <iomanip>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
// Constants for menu choices
const int Regular = 1,
Clearance = 2,
Discount = 3,
QUIT_CHOICE = 4;
unsigned short choice;
do {
//Display the menu
cout<<"Type 1 if you would like to purchase Regular priced items"<<endl;
cout<<"Type 2 if you would like to purchase Clearance items"<<endl;
cout<<"Type 3 if you would like to purchase Special Discount items"<<endl;
cout<<"Type 4 if you would like to quit" <<endl;
//Declare and input the choice
cin>>choice;
// Validate the menu selection.
while (choice < Regular || choice > QUIT_CHOICE)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//Based upon the choice, proceed to next menu
switch(choice){
case 1:{
// Display the Regular priced menu
cout<<"Type A if you would like to purchase brand name Cookies "<<endl;
cout<<"Type B if you would like to purchase brand name Juice "<<endl;
cout<<"Type C if you would like to purchase brand name Cereal "<<endl;
cout<<"Type D if you would like to purchase Brand name Crackers "<<endl;
cout<<"Type E if you would like to purchase Brand name Oatmeal "<<endl;
//Declare and input choice
char choice1;
cin>>choice1;
//Based upon the choice, solve the particular problem
switch(choice1){
case 'A':{
// Ask the user how many cookies they want
cout<<"How many boxes of cookies would you like to buy?"<<endl;
int Cookies;
const float Cprice = 5.6, Tax = .0775;
float Price;
cin>>Cookies;
Price = (Cookies * Cprice) + (Cookies * Cprice * Tax);
cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
break;
}
case 'B':{
// Ask the user how many Juices they want
cout<<"How many cans of juice would you like to buy?"<<endl;
int Juice;
const float Jprice = 5.2, Tax = .0775;
float Price;
cin>>Juice;
Price = (Juice * Jprice) + (Juice * Jprice * Tax);
cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
break;
}
case 'C':{
// Ask the user how many boxes of Cereal they want
cout<<"How many boxes of cereal would you like to buy?"<<endl;
int Cereal;
const float Clprice = 5.2, Tax = .0775;
float Price;
cin>>Cereal;
Price = (Cereal * Clprice) + (Cereal * Clprice * Tax);
cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
break;
}
case 'D':{
// Ask the user how many boxes of crackers they want
cout<<"How many boxes of crackers would you like to buy?"<<endl;
int Crack;
const float Crackprice = 5.2, Tax = .0775;
float Price;
cin>>Crack;
Price = (Crack * Crackprice) + (Crack * Crackprice * Tax);
cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
break;
}
case 'E':{
// Ask the user how boxes oatmeal they want
cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
int Oats;
const float Oprice = 5.2, Tax = .0775;
float Price;
cin>>Oats;
Price = (Oats * Oprice) + (Oats * Oprice * Tax);
cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
break;
}
default:{
cout<<"You do not know how to follow instructions, do you?"<<endl;
}
break;
}
break;
}
case 2:{
// Display the Clearance priced menu
cout<<"Type a if you would like to purchase clearance Cookies "<<endl;
cout<<"Type b if you would like to purchase clearance Juice "<<endl;
cout<<"Type c if you would like to purchase clearance Cereal "<<endl;
cout<<"Type d if you would like to purchase clearance Crackers "<<endl;
cout<<"Type e if you would like to purchase clearance Oatmeal "<<endl;
//Declare and input choice
char choice2;
cin>>choice2;
//Based upon the choice, solve the particular problem
switch(choice2){
case 'a':{
// Ask the user how many cookies they want
cout<<"How many boxes of cookies would you like to buy?"<<endl;
int ClearCookies;
const float OCprice =5.6, ClCprice = 4.0, Tax = .0775;
float Oprice, Price, Diff, Savings; // Diff = difference in price, Savings = % you saved
cin>>ClearCookies;
Price = (ClearCookies * ClCprice) + (ClearCookies * ClCprice * Tax);
Oprice =(ClearCookies * OCprice) + (ClearCookies * OCprice * Tax);
Diff = Oprice - Price;
Savings = 100 - (Price / Oprice) * 100 ;
cout<<"The price of the cookies are : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Savings<<" %"<<endl;
break;
}
case 'b':{
// Ask the user how many Juices they want
cout<<"How many cans of juice would you like to buy?"<<endl;
int ClJuice;
const float OJJprice = 5.2, ClJprice = 3.6, Tax = .0775;
float Price, OJYPrice, Diff, Sav;
cin>>ClJuice;
Price = (ClJuice * ClJprice) + (ClJuice * ClJprice * Tax);
OJYPrice = (ClJuice * OJJprice) + (ClJuice * OJJprice * Tax);
Diff = OJYPrice - Price;
Sav = 100 - (Price / OJYPrice) * 100;
cout<<"The price of the Juice is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
break;
}
case 'c':{
// Ask the user how many boxes of Cereal they want
cout<<"How many boxes of cereal would you like to buy?"<<endl;
int ClCereal ;
const float OClprice = 5.2, ClCrprice = 2, Tax = .0775;
float Price, Ocer, Diff, Sav; // Ocer=OriginalCeral Price
cin>>ClCereal;
Price = (ClCereal * ClCrprice) + (ClCereal * ClCrprice * Tax);
Ocer = (ClCereal * Ocer) + (ClCereal * Ocer * Tax);
Diff = Ocer - Price;
Sav = 100 - (Price/ Ocer) * 100;
cout<<"The price of the cookies are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
break;
}
case 'd':{
// Ask the user how many boxes of crackers they want
cout<<"How many boxes of crackers would you like to buy?"<<endl;
int ClCrack;
const float OCrackprice = 5.2, ClCrackprice = 1.9, Tax = .0775;
float Ocrack, Price, Diff, Sav;
cin>>ClCrack;
Price = (ClCrack * ClCrackprice) + (ClCrack * ClCrackprice * Tax);
Ocrack = (ClCrack * OCrackprice) + (ClCrack * OCrackprice * Tax);
Diff = Ocrack - Price;
Sav = 100 - (Price/Ocrack) * 100;
cout<<"The price of the crackers are: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
break;
}
case 'e':{
// Ask the user how boxes oatmeal they want
cout<<"How many boxes of oatmeal would you like to buy?"<<endl;
int ClOats;
const float Oprice = 5.2, ClOprice = 2.3, Tax = .0775;
float Price, Ooats, Diff, Sav;
cin>>ClOats;
Price = (ClOats * ClOprice) + (ClOats * ClOprice * Tax);
Ooats= (ClOats * Oprice) + (ClOats * Oprice * Tax);
Diff = Ooats - Price;
Sav = 100 - (Price / Ooats) * 100;
cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
cout<<"You saved $ " <<fixed<<setprecision(2)<<Diff<<" "<<endl;
cout<<"Thats a savings of " <<fixed<<setprecision(0)<<Sav<<" %"<<endl;
break;
}
default:{
cout<<"You do not know how to follow instructions, do you?"<<endl;
}
break;
}
break;
}
case 3:{
// Display another menu
cout<<"Press Q if you would like to buy Cheese"<<endl;
cout<<"Press W if you would like to buy Milk"<<endl;
cout<<"Press E if you would like to buy Candy"<<endl;
cout<<"Press R if you would like to buy Tea"<<endl;
cout<<"Press T if you would like to buy Water"<<endl;
//Declare and input the choice
char choice3;
cin>>choice3;
//Based upon the choice, proceed to next menu
switch(choice3){
case 'Q':{
// Ask the user how much cheese
cout<<"How much cheese would you like to buy?"<<endl;
int Cheese;
const float Cheeseprice = 3.99, Tax = .0775;
float Price;
cin>>Cheese;
if (Cheese ==1) {
Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax) -1;
cout<<"The price of the cheese after discount is $"<<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else {
Price = (Cheese * Cheeseprice) + (Cheese * Cheeseprice * Tax);
cout<<"The price of the cheese is : $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
break;
}
case 'W':{
// Ask the user how much Milk they want
cout<<"How many gallons of Milk would you like to buy?"<<endl;
int Milk;
const float Mprice = 3.39, Tax = .0775;
float Price;
cin>>Milk;
if (Milk >= 5 || Milk <= 10) {
Price = (Milk * Mprice);
cout<<"The price of the milk after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else {
Price = (Milk * Mprice) + (Milk * Mprice * Tax);
cout<<"The price of the milk is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
break;
}
case 'E':{
// Ask the user how many boxes of Candy they want
cout<<"How many boxes of Candy would you like to buy?"<<endl;
int Candy;
const float CanPrice = 1.2, Tax = .0775;
float Price;
cin>>Candy;
if (Candy > 9) {
Price = ((Candy * CanPrice) + (Candy * CanPrice * Tax)) - ((CanPrice * Tax ) + CanPrice) ;
cout<<"The price of the candy after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else {
Price = (Candy * CanPrice) + (Candy * CanPrice * Tax) ;
cout<<"The price of the candy is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
break;
}
case 'R':{
// Ask the user how many tea packets they want
cout<<"How many packets of tea would you like to buy?"<<endl;
int Tea;
const float Tprice = 2.89, Tax = .0775;
float Price;
cin>>Tea;
if (Tea <4) {
Price = (Tea * Tprice) + (Tea * Tprice * Tax) * .50;
cout<<"The price of the Tea after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else{
Price = (Tea * Tprice) + (Tea * Tprice * Tax);
cout<<"The price of the Tea is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
break;
}
case 'T':{
// Ask the user how gallons of water they want
cout<<"How many gallons would you like to buy?"<<endl;
int Wat;
const float Wprice = 1.0, Tax = .0775;
float Price;
cin>>Wat;
if (Wat < 3, Wat > 1)
{
Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .05;
cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else if (Wat < 4 , Wat > 2)
{
Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .06;
cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else if (Wat >3)
{
Price = (Wat * Wprice) + (Wat * Wprice * Tax) * .07;
cout<<"The price of the Water after discount is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
else {
Price = (Wat * Wprice) + (Wat * Wprice * Tax);
cout<<"The price of the oatmeal is: $ " <<fixed<<setprecision(2)<<Price<<" "<<endl;
}
break;
}
default:{
cout<<"You do not know how to follow instructions, do you?"<<endl;
}
break;
}
break;
}
default:{
cout<<"Why don't you want to shop anymore?"<<endl;
}
}
} while (choice!= QUIT_CHOICE);
return 0;
}

New Topic/Question
Reply



MultiQuote



|