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
|
/*Vending Machine Program (Homework 5)
Allow for 0 to many products to be added.
Use a product structure to contain a Name, Price, and Description.
Every time a product is added, print all products to a file.
Every time the program is started, read in all products from a file
Continue to make sure that everything is sorted.
In main, have a basic state machine using enums for a Main Menu that allows you to:
a) Add a new product
b) sort products
c) Display products
d) Exit program*/
#include <iostream>;
#include <string>;
#include <vector>;
#include <fstream>;
//#include "Main.h";
using namespace std;
enum MainMenu
{
AddProduct,
SortProducts,
DisplayProducts,
ExitProgram
};
struct Product
{
string Name;
string Description;
double Price;
};
void PrintProduct()
{}
Product GetProductFromInput()
{
Product temp;
cout << "Enter the name of a product: ";
//cin >> productName.Name;
getline(cin, temp.Name);
cout << "Enter the description of the product: ";
//cin >> productDescription.push_back;
getline(cin, temp.Description);
cout << "Enter the price for the product: ";
//cin >> productPrice.push_back;
cin >> temp.Price;
return temp;
}
int main()
{
//Reads all products from the "product.txt" file
ifstream productFileInput;
productFileInput.open("productFile.txt");
if (productFileInput.is_open && productFileInput.eof)
{
string temp;
getline(productFileInput, temp);
cout << temp << "\n";
}
productFileInput.close();
double customerMoney = 0.00;
cout << "How much money do you have? ";
cin >> customerMoney;
while (customerMoney >= 0.00)
{
//string productName;
vector <string> productName;
//string productDescription;
vector <string> productDescription;
//double productPrice = 0.00;
vector <double> productPrice;
//int customerChoice[4] = { 0, 1, 2, 3 };
//vector <int> customerChoice;
vector <Product> product;
int exitProgram = 0;
GetProductFromInput();
//Prints products to the "product.txt" file
ofstream productFileOutput;
productFileOutput.open("productFile.txt");
productFileOutput << "Product Name: " << productName << "\n" << "Product Description: " << productDescription << "\n" << "Product Price: " << productPrice << "\n";
productFileOutput.close();
//for (customerChoice[0] = 1; customerChoice[0] < 4; customerChoice[0]++)
for (product = product.begin; product < product.end; product++)
{
cout << "You have selected: " << productName.back << "\n";
customerMoney = customerMoney - productPrice.back;
cout << "You have $" << customerMoney << " left\n";
cout << "Would you like to exit the program? (Enter '1' for true or '2' for false)";
cin >> exitProgram;
if (exitProgram == 1)
{
return 0;
}
else if (exitProgram == 2)
{
continue;
}
}
}
cin.ignore();
cin.get();
return 0;
}
|