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 126 127 128 129 130 131 132 133 134 135 136 137
|
/* This program will simulate a vending machine program in a semi-real world application. It is to read from a text file "machine.txt" and store the information
into an array of structures <structs>. The program will enter a loop and interactively display the names and prices of the drinks as well as an option to quit.
The user will select a drink or quit. If any invalid input is detected it must be alerted to the user and wait until proper input is recieved. All input must be
echoprinted. If a drink is selected, ask the user to "insert" $1.00 and display the "change" as well as decrement the quantity of the soda selected. If the
quantity was zero, display SOLD OUT and allow the user to try again. When the quit option is chosen, display the final values of all structs showing name, price,
and quantity. Also show the profit as total cans sold multiplied by $0.25. Finally, an array must be displayed showing the sodas organized by highest to lowest in
quantity of cans.*/
#include <iostream> // Header Files
#include <fstream>
#include <string>
#include <iomanip>
using namespace std; // Name space
struct sodaInfo // the qualities of each soda
{
string name;
double price;
int quantity;
};
const int NUM_SODAS = 6; // number of types of soda
const double DOLLAR = 1.00; // amount of cash inserted every time
const double PROFIT = 0.25; // amount of profit per can sold
const int MAX_CANS = 20; // maximum number of cans that can be held per machine
typedef sodaInfo sodaMenu[NUM_SODAS]; // defining the array sodaMenusodaMenu[NUM_SODAS]; // declaring the struct sodaMenu
int quantityArray[NUM_SODAS]; // defining the array for sorting quantities
void storeData(sodaMenu, ifstream &); // function will read the file and put data into an array of structs
void displayMenu(sodaMenu); // function will enter a do while loop to display names and prices of sodas from the struct array and await user input
int userChoice(sodaMenu, int); // function will displayMenu for the user to interactively decide sodas 1-6 or 0 which quits and prints a final report
void showProfit(int); // function will calculate the total profit with cansSold * PROFIT
void buildQuantityArray(sodaMenu, int quantityArray[], int); // function will use sodaMenu to build new array of just quantities to be sorted
int findMaxValue(int quantityArray[NUM_SODAS]); // function will find the maximum number to sort the new array
void sortQuantityArray(sodaMenu, int quantityArray[], int); // sorts the array of quantities into highest to lowest.
void finalStatus(sodaMenu); // this function shows the names, prices, and quantities of the machine at the end of the iteration.
int findMax(sodaMenu); // one dimensional array sorting from highest to lowest in quantity of the sodas sold, and then a final table showing
// the soda name, quantity, and price.
int main()
{
ifstream sodaList; // declaring the list of names, prices, and quantities of sodas. Naming it SodaList.open("machine.txt");
sodaList.open("C:\\Users\\Dustin\\Documents\\Programming Proj ects\\vendingMachine\\vendingMachineP6"); // hard copying file location
sodaMenu machineList; // defining the array as machineList
int userInput; // the integer showing the choice of the user
int cansSold = 0; // the running quantity that will grow with each soda purchased to be used in calculating the total profit.
int maxValue = 0; // final maxvalue
if (sodaList) // checking to see if file loaded properly
{
cout << "File successfully open\n"; // let the user know the file opened properly
storeData(machineList, sodaList); // put the data into the array of structs
do
{
displayMenu(machineList); // print the initial data of the vending machine in a do while loop and await user input
userInput = userChoice(machineList, cansSold); // switch that lets the user interactively choose a soda, display change, change quantities, and quit.
} while (userInput != 0);
if (userInput == 0)
quantityArray[NUM_SODAS];
showProfit(cansSold); // calling function to show profit
buildQuantityArray(machineList, quantityArray, cansSold); // calling function to transfer quantity info from sodaList to new array quantityArray[]
findMaxValue(quantityArray); // calling function to find the maximum value
sortQuantityArray(machineList, quantityArray, maxValue); // calling function to sort the quantityArray[] into highest to lowest
finalStatus(machineList); // finally display a table showing the final results of names, prices, and quantities in machine
sodaList.close(); // close the file
cout << "Terminating Program...";
cin.get();
}
else
{
cout << "\nError opening file!\n"; //
}
return 0;
}
void storeData(sodaMenu sodaMenu, ifstream &sodaList)
{ /* function storeData */
for (int i = 0; i < NUM_SODAS; i++)
{
getline(sodaList, sodaMenu[i].name);
std::setprecision(3);
sodaList >> sodaMenu[i].price;
sodaList >> sodaMenu[i].quantity;
sodaList.ignore(10, '\n');
}
} /* function storeData */
void displayMenu(sodaMenu sodaList)
{ /* function displayMenu */
int i; // counter to allow additional sodas to be added if needed. It will print line by line the name and price of each soda.
cout << endl;
cout << setfill('*') << setw(25);
cout << "Thirsty? Try your refreshing soda today!\n";
cout << setw(25) << setfill(' ');
for (i = 0; i < NUM_SODAS; i++) // enter loop to print NUM_SODAS names and prices for the user to select.
{
cout << "[" << (i + 1) << "] " << sodaList[i].name << setw(5) << sodaList[i].price << endl;
}
cout << "[0] Quit";
} /* function displayMenu */
int userChoice(sodaMenu sodaList, int cansSold) // sending the array and the cansSold which starts at 0.
{ /* function userChoice */
int userInput = -1; // initializing the user choice as NOT 0, or 1-6
cin >> userInput; // user inputs their choice
if (userInput == 0)
cout << "You have chosen 0! Quitting vending operations...";
else if (userInput >= 1 && userInput <= 6)
{
if (sodaList[userInput - 1].quantity > 0) // checking to make sure the quantity is there
{
cout << "Please insert $1.00 -=-=-=-=- $$$ Dollar inserted"; // pretending the user is inserting $1.00
cout << "You have chosen 1! " << sodaList[userInput-1].name << "!\n"; // echoprinting the user's decision
cout << "Your change is " << std::setprecision(3) << (DOLLAR - sodaList[userInput-1].price) << "!"; // calculating the change.
sodaList[userInput-1].quantity--; // removing one from the quantity of sodas in the appropriate array
cansSold++; // add one to number of cans sold
}
else
cout << "\nSorry! We are SOLD OUT! Please select another choice!\n"; // letting user know the machine is sold out of this soda
}
cout << "\nInvalid Entry, please enter a value 0-6."; // value was not an integer 0-6 so it is invalid
return userInput; // letting main function know what soda was selected
} /* function userChoice */
void showProfit(int cansSold)
{ /* function showProfit */
int totalCansSold = cansSold;
cout << "\n The total profit for this business day is " << totalCansSold * PROFIT << ".\n";
} /* function showProfit */
void buildQuantityArray(sodaMenu machineList, int quantityArray[])
{ /* function buildQuantityArray */
int i;
for (i = 0; i < NUM_SODAS; i++)
quantityArray[i] = machineList[i].quantity;
} /* function buildQuantityArray */
|