declare ALL variables at the beginning of the program to make the program easier to read and understand

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

declare ALL variables at the beginning of the program to make the program easier to read and understand.

(please answer it). 

 

Expert Solution
Source Code

#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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education