HELP! Vending Machine Program (C++)

Where to begin, well for starters I was originally supposed to create a vending machine program that allowed a user to enter 4 products (names and prices) and I did not have to support names with spaces and I also did not have to worry about input validation. I did all of these things in the code below and the program runs fine however, now I have to change up this code to allow for 0-to-many products to be entered, I need to implement a product structure to contain a Name, Price, and Description, and every time a product is added I need to print all the products to a file, and every time the program is started I need to read in all products from a file, and I need to make sure that everything is sorted, and last but not least In main to 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

Could someone please help me make these changes =(

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;
}
I would read the entire file into memory. Then operate on the data in memory as it's input. That way you can have the data sorted in memory as it's added.

Then when the option to exit is selected you can write the data back into a file.

If you want to print the entire data to the screen as it's sorted from input you could do that without having to read it from the file if it remains in memory. Seems a bit much to write to a file and read it back just to rewrite to the file for every product added.
Last edited on
Topic archived. No new replies allowed.