declare ALL variables at the beginning of the program to make the program easier to read and understand
declare ALL variables at the beginning of the
(please answer it).
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float billAmount, inputAmount, currentInput, changeToBeReturned;
int choice=0;
char lightOrRegular;
string item="";
const float WATER=1.25, LEMONADE=1.35, COLA=1.70, ROOTBEAR=1.55;
//display the menu
cout << "1- Bottled-water $1.25" << endl;
cout << "2- Lemonade $1.35" <<endl;
cout << "3- Cola $1.70" << endl;
cout << "4- Root-bear $1.55" << endl;
cout << "5- Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
//this loop iterates if choice greater than 5
while(choice>5){
cout << "You entered an invalid choice! Try again" << endl;
cout << "Enter your choice: ";
cin >> choice;
}
if(choice!=5){ //if you don't want to exit
if(choice==1){
billAmount=WATER; //billAmount is assigned to WATER value
item="Bottled-water" ; //item is assigned with Bottled-water
}
else{
cout << "Enter L for Light or R for Regular: ";
cin >> lightOrRegular;
//this loop iterates till you enter a valid choice Regular or Light
while(lightOrRegular!='L' && lightOrRegular!='R'){
cout << "You entered an invalid choice! Try again" << endl;
cout << "Enter L for Light or R for Regular: ";
cin >> lightOrRegular;
}
if(choice==2){
billAmount=LEMONADE; //billAmount is assigned to LEMONADE value
if(lightOrRegular=='L')
item="Light Lemonade";
else
item="Regular Lemonade";
}
else if(choice==3){
billAmount=COLA; //billAmount is assigned with COLA value
if(lightOrRegular=='L')
item="Light Cola";
else
item="Regular Cola";
}
else{
billAmount=ROOTBEAR;
if(lightOrRegular=='L')
item="Light RootBear";
else
item="Regular RootBear";
}
//This loop iterates till you input the amount greater than bill
while(inputAmount<billAmount && currentInput!=-1){
cout << "Enter " << float(billAmount-inputAmount) << "amount to proceed or -1 to quit" << endl;
cin >> currentInput;
if(currentInput!=-1)
inputAmount+=currentInput;
}
if(inputAmount>=billAmount){
//calculate change to be returned
changeToBeReturned=inputAmount-billAmount;
cout << "Here is your " << item << endl;
cout << "Paid:\t\t$"<< inputAmount << endl;
cout << "Change Back:\t$ " << changeToBeReturned << endl;
//finding number of bills
//fmod is used to operate modulus operation on float numbers
//displaying bills only if that category is greater than 0
if(changeToBeReturned/5 >0){
cout << "\t\t" << (int)changeToBeReturned/5 << " Five Dollar Bill" << endl;
changeToBeReturned = fmod(changeToBeReturned,5);
}
if(changeToBeReturned/1 >0){
cout << "\t\t" << (int)changeToBeReturned/1 << " Dollar Bill" << endl;
changeToBeReturned = fmod(changeToBeReturned,1);
}
if((int)(changeToBeReturned/0.25) >0){
cout << "\t\t" << (int)(changeToBeReturned/0.25) << " Quarter" << endl;
changeToBeReturned = fmod(changeToBeReturned,0.25);
}
if((int)(changeToBeReturned/0.1) >0){
cout << "\t\t" << (int)(changeToBeReturned/0.1) << " Dimen" << endl;
changeToBeReturned = fmod(changeToBeReturned,0.1);
}
if((int)(changeToBeReturned/0.05) >0){
cout << "\t\t" << (int)(changeToBeReturned/0.05) << " Nickel" << endl;
changeToBeReturned = fmod(changeToBeReturned,0.05);
}
if((int)(changeToBeReturned/0.01) >0){
cout << "\t\t" << (int)(changeToBeReturned/0.01) << " Penny" << endl;
changeToBeReturned = fmod(changeToBeReturned,0.01);
}
}
}
}
else{
cout << "Goodbye!" << endl; //if 5 is entered
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images