Soda Vending Machine function issues

Im getting a lot of errors in my code and whenever i try to fix or change something I am getting even more errors. I started to write this program myself, but halfway through i began to use my teacher's template. I keep getting cout is ambiguous along with other errors including syntax errors, missing declarations and identifiers. Im feeling overwhelmed and dont know what to do.



*EDIT*
I just need help with my incomplete function. I do not understand how to access the structure and how to decrease the supply!! Thanks for any help



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
  //Chapter 11 Program 13 Vending Machine Drink Simulator
// Written by 

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string> 

using namespace std; 

const int numDrinks = 5; //number of different drinks in vanding machine 

struct Drink
{
	string name;			// Drink name
	double price;    		// Price of the drink
	int num;			    // Number of cans in the machine
};


//*************************************************************************************************
//Get choice function. return that value - 1 due to subscript numbering                           *
//*************************************************************************************************
int getChoice(Drink m[])
{
	int choice;
	cout << " Please select a drink by pressing on the corresponding number" << endl;
	cout << " 1. Cola: .75cents " << endl;
	cout << " 2. Root Beer : .75cents " << endl;
	cout << " 3. Lemon - Lime: .75cents " << endl;
	cout << " 4. Grape Soda : .80cents " << endl;
	cout << " 5. Cream Soda : .80cents " << endl;
	cout << (numDrinks + 1) << " Leave the drink machine\n\n";
	cin >> choice; 

	if (choice > 6 || choice < 1)
	{
		cout << " Please choose a valid option. ";
	}
	return numDrinks;
}

//**************************************************************************************************
//Process Transaction: Process user choice and update earning variable                             *
//**************************************************************************************************
void ProcessTransaction(Drink m[], int choice, double &earnings)

int choice;
int i;
bool soldout = true;

while ((choice < 1 || choice > 6) || soldout)
{
	cout << " Please select a drink by pressing on the corresponding number" << endl;
	cout << " 1. Cola: .75cents " << endl;
	cout << " 2. Root Beer : .75cents " << endl;
	cout << " 3. Lemon - Lime: .75cents " << endl;
	cout << " 4. Grape Soda : .80cents " << endl;
	cout << " 5. Cream Soda : .80cents " << endl;
	cout << (numDrinks + 1) << " Leave the drink machine\n\n";
}

//**********************************************************************************************************
//Main                                                                                                     *
//**********************************************************************************************************
int main()
{
	// local variables
	//
	int 	choice = 0;					// Menu choice
	double 	earnings = 0.0;					// Drink machine earnings
	Drink 	machine[numDrinks] = 						// Array of drinks in the machine
	{
		// name				cost	quantityAvailable
		// -----------		----	-----------------
		{ "Cola     ", 0.75, 20 },
		{ "Root Beer", 0.75, 20 },
		{ "Lemon-Lime", 0.75, 20 },
		{ "Grape Soda", 0.80, 20 },
		{ "Cream Soda", 0.80, 20 }
	};

	// initialization
	cout << fixed << showpoint << setprecision(2);		// Set the floating-point output formatting.

	// Display the menu and process the user's choice.
	do
	{
		choice = GetChoice(machine);							// get the customer's choice
		if (choice != numDrinks)
			ProcessTransaction(machine, choice, earnings);	// Process the transaction.
	} while (choice != numDrinks);

	// Display the machine's total earnings
	//
	cout << "Total earnings: $" << earnings << endl;
	return 0;
}
Last edited on
Put an opening brace { on line 47 and put a closing brace } on line 62. That should cut down a ton of errors.
Thank you! I really really appreciate your prompt reply!!! i did not notice i accidentally erased one brace. Im still getting a lot of errors even though i removed about 7. I am still getting the cout ambiguous error on lines 27-33, 38, and 55-61. Am i just forgetting to put braces?
Last edited on
int choice shadows the int choice in the parameter list of ProcessTransaction() and should be removed.
int i is not used in ProcessTransaction()
And in main GetChoice(machine) should be getChoice(machine) as c++ is case sensitive.
And ProcessTransactions doesn't seem to be doing anything.
You need it clear in your head the steps required before coding.
Last edited on
I was writing the program myself before using my teacher's template which is why there are some extra things not quite where they belong. I have opted to scrap what i did so far and start over from my teachers template. I need help on the processing function and how to determine if there is still a supply of soda in the machine. This is my code now. I need help starting with line 117. i get confused with how to point into struct to show the price.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/****************************************************************************************

Chapter 11, Programming Challenge 13: Drink Machine Simulator

Written by: 

****************************************************************************************/

// ************************************************************************************** 
// INCLUDES
// ************************************************************************************** 

// system
//
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>


using namespace std;

// ************************************************************************************** 
// CONSTANTS
// ************************************************************************************** 

const int NUM_DRINKS = 5;

// ************************************************************************************** 
// STRUCTURES
// ************************************************************************************** 

struct Drink
{
	string 	name;			// Drink name
	double	price;    		// Price of the drink
	int 	num;         	// Number of cans in the machine
};
Drink Beverage;

// ************************************************************************************** 
// FUNCTIONS
// ************************************************************************************** 


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// GetChoice
//				This function gets the user's choice and returns that value minus one. 
//				This adjustment is made because the value will be used as a subscript 
// 				into the Drink array by the transaction function.
//
//
int GetChoice(Drink m[])
{
	// local constants
	//
	
	// local variables
	//
	int choice;
	
	// Display a list of drinks preceded by a number. This will be the menu.
	// cout << "Put code here to DISPLAY the list of available drinks and prices" << endl;
	cout << " Please select a drink by pressing on the corresponding number" << endl;
	cout << " 1. Cola: .75cents " << endl;
	cout << " 2. Root Beer : .75cents " << endl;
	cout << " 3. Lemon Lime: .75cents " << endl;
	cout << " 4. Grape Soda : .80cents " << endl;
	cout << " 5. Cream Soda : .80cents " << endl;
	

	// Display the last menu item, which is to leave the drink machine.
	//
	cout << (NUM_DRINKS + 1) << " Leave the drink machine\n\n";

	// Get the user's choice.
	//
	cin >> choice;
	// Validate the choice.
	//
	if (choice > 6 || choice < 1)
	{
		cout << " Please choose a valid option. ";
	}
	// Return the choice
	//
	return NUM_DRINKS;		/* THIS TEMPLATE JUST RETURNS THE QUIT CHOICE*/
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// ProcessTransaction
//				Process the user's choice and update the earnings variable
//
//
void ProcessTransaction(Drink m[], int choice, double &earnings)
{
	float moneyRecieved;
	float change; 
	// If the selected drink is sold out, display a message and get out of this function.
	// otherwise
	// Get some money from the customer. (ie. have the user input up to 1.00)
	//
	cout << " Please enter an amount less than $1.00 to pay for your drink. ";
	cin >> moneyRecieved;

	// Make sure the customer entered at least enough for the selected drink, and no more than $1.00.
	// 
	if (moneyRecieved < 0 || moneyRecieved > 1)
	{
		cout << " Sorry. This vending machine does not accept money over $1.00. ";
	}
	// Process the selection and give back any change that is due.
	//
	change = moneyRecieved - Beverage.price - 1;
	// if theMoneyReceived >= thePriceOfTheSelectedItem
	{
		// Dispense the drink message

		// Calculate any change that is due.

		// If change is due, give it to the customer.

		// Update our earnings.

		// Decrease the number of cans of the selected drink currently in the machine.

		// Display the number of cans of this drink currently in the machine.
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// main
//
//
int main()
{
	// local variables
	//
	int 	choice = 0;					// Menu choice
	double 	earnings = 0.0;					// Drink machine earnings
	Drink 	machine[NUM_DRINKS] = 						// Array of drinks in the machine
	{
		// name				cost	quantityAvailable
		// -----------		----	-----------------
		{ "Cola     ", 0.75, 20 },
		{ "Root Beer", 0.75, 20 },
		{ "Lemon-Lime", 0.75, 20 },
		{ "Grape Soda", 0.80, 20 },
		{ "Cream Soda", 0.80, 20 }
	};

	// initialization
	//
	cout << fixed << showpoint << setprecision(2);		// Set the floating-point output formatting.

	// Display the menu and process the user's choice.
	//
	do
	{
		choice = GetChoice(machine);							// get the customer's choice
		if (choice != NUM_DRINKS)
			ProcessTransaction(machine, choice, earnings);	// Process the transaction.
	} while (choice != NUM_DRINKS);

	// Display the machine's total earnings
	//
	cout << "Total earnings: $" << earnings << endl;
	return 0;
}
Last edited on
This is where i am at now. I am having issues with line 127. Same area but i filled out more code but i still cant figure out what to do. This is where i am at.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/****************************************************************************************


Chapter 11, Programming Challenge 13: Drink Machine Simulator

Written by: 

****************************************************************************************/

// ************************************************************************************** 
// INCLUDES
// ************************************************************************************** 

// system
//
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>


using namespace std;

// ************************************************************************************** 
// CONSTANTS
// ************************************************************************************** 

const int NUM_DRINKS = 5;

// ************************************************************************************** 
// STRUCTURES
// ************************************************************************************** 

struct Drink
{
	string 	name;			// Drink name
	double	price;    		// Price of the drink
	int 	num;         	// Number of cans in the machine
};
Drink Beverage;

// ************************************************************************************** 
// FUNCTIONS
// ************************************************************************************** 


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// GetChoice
//				This function gets the user's choice and returns that value minus one. 
//				This adjustment is made because the value will be used as a subscript 
// 				into the Drink array by the transaction function.
//
//
int GetChoice(Drink m[])
{
	// local constants
	//
	
	// local variables
	//
	int choice;
	
	// Display a list of drinks preceded by a number. This will be the menu.
	// cout << "Put code here to DISPLAY the list of available drinks and prices" << endl;
	cout << " Please select a drink by pressing on the corresponding number" << endl;
	cout << " 1. Cola: .75cents " << endl;
	cout << " 2. Root Beer : .75cents " << endl;
	cout << " 3. Lemon Lime: .75cents " << endl;
	cout << " 4. Grape Soda : .80cents " << endl;
	cout << " 5. Cream Soda : .80cents " << endl;
	

	// Display the last menu item, which is to leave the drink machine.
	//
	cout << (NUM_DRINKS + 1) << " Leave the drink machine\n\n";

	// Get the user's choice.
	//
	cin >> choice;
	// Validate the choice.
	//
	if (choice > 6 || choice < 1)
	{
		cout << " Please choose a valid option. ";
	}
	// Return the choice
	//
	return choice;		/* THIS TEMPLATE JUST RETURNS THE QUIT CHOICE*/
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// ProcessTransaction
//				Process the user's choice and update the earnings variable
//
//
void ProcessTransaction(Drink m[], int choice, double &earnings)
{
	float moneyRecieved;
	float change;
	// If the selected drink is sold out, display a message and get out of this function.
	// otherwise
	// Get some money from the customer. (ie. have the user input up to 1.00)

	cout << " Please enter an amount less than $1.00 to pay for your drink. ";
	cin >> moneyRecieved;

	// Make sure the customer entered at least enough for the selected drink, and no more than $1.00.
	
	if (moneyRecieved < 0 || moneyRecieved > 1)
	{
		cout << " Sorry. This vending machine does not accept money over $1.00. ";
	}
	// Process the selection and give back any change that is due.
	
	
	if (moneyRecieved >= Beverage.price)
	{
		cout << "Your money is being processed. Your drink will be dispensed shortly. ";
	}
	{
		// Dispense the drink message

		// Calculate any change that is due.
	change = moneyRecieved - Drink[choice + 1].price;
		// If change is due, give it to the customer.
		cout << "Here is your change: " << change << " Thank you for purchasing a drink! ";
		// Update our earnings.
		earnings = moneyRecieved - change; 
		// Decrease the number of cans of the selected drink currently in the machine.

		// Display the number of cans of this drink currently in the machine.
	}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// main
//
//
int main()
{
	// local variables
	//
	int 	choice = 0;					// Menu choice
	double 	earnings = 0.0;					// Drink machine earnings
	Drink 	machine[NUM_DRINKS] = 						// Array of drinks in the machine
	{
		// name				cost	quantityAvailable
		// -----------		----	-----------------
		{ "Cola     ", 0.75, 20 },
		{ "Root Beer", 0.75, 20 },
		{ "Lemon-Lime", 0.75, 20 },
		{ "Grape Soda", 0.80, 20 },
		{ "Cream Soda", 0.80, 20 }
	};

	// initialization
	//
	cout << fixed << showpoint << setprecision(2);		// Set the floating-point output formatting.

	// Display the menu and process the user's choice.
	//
	do
	{
		choice = GetChoice(machine);							// get the customer's choice
		if (choice != NUM_DRINKS)
			ProcessTransaction(machine, choice, earnings);	// Process the transaction.
	} while (choice != NUM_DRINKS);

	// Display the machine's total earnings
	//
	cout << "Total earnings: $" << earnings << endl;
	return 0;
}
Last edited on
1
2
3
4
5
6
7
void ProcessTransaction(Drink m[], int choice, double &earnings)
{
    ...

	    change = moneyRecieved - m[choice+1].price;
    ...
}


Suggestion is to type the type of error thrown up by the compiler into google.
Last edited on
Topic archived. No new replies allowed.