12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
#include <iostream> #include <iomanip> #include <conio.h> using namespace std; void MainMenu(); double Change(int&); void UserOption(char); int UserAmount(int); int main() { //Declaring int bill = 0; int userBalance = 0; char user_option; MainMenu(); // Shows main menu //Gets User Choice do { user_option = _getch(); // Gets user Input on keypressed UserOption(user_option); // Displays the amount due }while(user_option > 5 && user_option < 1); //Calculates the Change userBalance = UserAmount(bill); // Adds the bill to the userBalance if(userBalance < 1.75){ cout << "Add more funds." << endl; while(userBalance < 1.75){ cout << endl; userBalance += UserAmount(bill); } } cout << Change(userBalance); // Calculates the Change due to user cout << endl; cout << "Thanks for Purchasing here!" << endl << endl; return 0; } void MainMenu(){ cout << "Cola Machine" << endl << endl; cout << "---------------" << endl; cout << "1." << setw(11) << "Coca-Cola" << endl << "2." << setw(8) << "sprite" << endl << "3." << setw(13) << "Orange Coke" << endl << "4." << setw(15) << "Vitamin Water" << endl << "5." << setw(7) << "Water" << endl; } double Change(int& userBalance){ int bill = 0; //If user tries to not input anything if(userBalance < 0){ cout << "I'm sorry you must enter a valid bill." << endl; return -1; } //Calculates the Change due. else return userBalance - 1.75; } int UserAmount(int bill){ cout << "Enter your bill amount: "; do{ cin >> bill; }while(bill < 1); return bill; // Return bill, will be added to userBalance } void UserOption(char user_option){ switch(user_option) { case '1': cout << "Coca-Cola -> $1.75" << endl; break; case '2': cout << "Sprite -> $1.75" << endl; break; case '3': cout << "Orange Coke -> $1.75" << endl; break; case '4': cout << "Vitamin Water -> $1.75" << endl; break; case '5': cout << "Water -> $1.75" << endl; break; default: cout << "Enter a valid option 1-5" << endl; break; } }