1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Function Prototypes
//int validateUserInput(int);
int displayDrinksAndGetUserChoice(const string[],
const double[],
const int[],
int);
void updateBeverageQty(int, int[]);
int main()
{
const int MAX_NUM_DRINKS = 4;
string beverage[MAX_NUM_DRINKS] = {
"Coca-Cola",
"Sprite",
"Gatorade",
"Spring-Water"
};
double price[] = {
0.75, // Coca-Cola
0.65, // Sprite
0.95, // Gatorade
1.25 }; // Spring-Water
int qtyOnHand[] = {
20, // Coca-Cola
20, // Sprite
20, // Gatorade
20 }; // Spring-Water
int selectedDrink;
char userAnswer;
do
{
//selectedDrink = validateUserInput(userChoice);
selectedDrink = displayDrinksAndGetUserChoice(beverage, price, qtyOnHand, MAX_NUM_DRINKS);
switch (selectedDrink)
{
case 1:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
break;
case 2:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
break;
case 3:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
break;
case 4:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
break;
default:
cout << "Please enter 1,2,3 or 4 only!" << endl;
}
cout << "Do you want to go again? (Y/n):";
cin >> userAnswer;
}
while ((userAnswer == 'Y') || (userAnswer == 'y'));
system("PAUSE");
return 0;
}
//Function to get user choice
int displayDrinksAndGetUserChoice(const string drink[],
const double price[],
const int qty[],
int arrSize)
{
int userChoice=0;
cout << "Welcome to COP 1334 Vending Machine" << endl;
cout << "Please choose from one of the following refreshing options\n" << endl;
cout << left;
// HEADER
cout << setw(23) << std::left << "-__DRINK NAME_________-"
<< setw(8) << std::left << "-___PRICE"
<< setw(12) << std::left << "-QUANTITY-"
<< endl;
for (int i = 0; i < arrSize; i++)
{
cout << setw(1) << std::left << i + 1
<< setw(2) << std::left << ". "
<< setw(23) << std::left << drink[i]
<< setw(2) << std::left << "$"
<< setw(8) << std::left << price[i]
<< setw(12) << std::left << qty[i]
<< endl;
}
cout << "\nYour choice: ";
cin >> userChoice;
{
while ((!(cin >> userChoice) || ((userChoice < 5) && (userChoice < 1))))
cin.clear();
cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
char ch;
while (cin.get(ch) && ch != '\n');
}
return userChoice;
}
//Function to update beverage quantity
void updateBeverageQty(int index, int beverageQty[])
{
beverageQty[index]--;
}
|