Vending Program Help

Hello, when I run this program I am getting an error on my ValueOfSnacks functions stating s1 - s4 weren't declared in scope. I am really confused as to how I am supposed to get the snack price from my snack object over to miniVend object. For clarity sake, I have a Snack, VendSlot & MiniVend class. My MiniVend is supposed to return the total value of all snacks in the machine. Let me know if you need additional code.


miniVend.h
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
  #ifndef MINIVEND
#include <string>
#include "VendSlot.h"
#include "Snack.hpp"
using std::string;

class miniVend
{
public:
	miniVend(VendSlot, VendSlot, VendSlot, VendSlot, double); //constructor
	int numEmptySlots();
	miniVend(Snack, Snack, Snack, Snack);
	double valueOfSnacks(); 
	//void buySnack(int);
	double getMoney();
	~miniVend(); //desructor


private:
	VendSlot vendslot1; //declare all the vending slots.
	VendSlot vendslot2; //declare all the vending slots.
	VendSlot vendslot3; //declare all the vending slots.
	VendSlot vendslot4; //declare all the vending slots.
	double moneyInMachine; //money in the machine

};
#endif // !MINIVEND



MiniVend.cpp
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
#include "miniVend.h"
#include "Snack.hpp"
#include "VendSlot.h"

miniVend::miniVend(VendSlot vs1, VendSlot vs2, VendSlot vs3, VendSlot vs4, double)
{
	vendslot1 = vs1;
	vendslot2 = vs2;
	vendslot3 = vs3;
	vendslot4 = vs4;
	double moneyInMachine = 0;
	
}

miniVend::miniVend(Snack s1, Snack s2, Snack s3, Snack s4)
{

	double snack1Price = s1.getSnackPrice();
	double snack2Price = s2.getSnackPrice();
	double snack3Price = s3.getSnackPrice();
	double snack4Price = s4.getSnackPrice();
} 
int miniVend::numEmptySlots()
{
	int vend1 = vendslot1.getAmount();
	int vend2 = vendslot2.getAmount();
	int vend3 = vendslot3.getAmount();
	int vend4 = vendslot4.getAmount();
	
	
	int counter = 0;

	if (vend1 == 0)
	{
	    counter++;
	}
	else if (vend2 == 0)
	{
		counter++;
	}
	else if (vend3 == 0)
	{
		counter++;
	}
	else if (vend4 == 0)
	{
		counter++;
	}
	
	return counter;
		


}

double miniVend::valueOfSnacks()
{

	double valueVend1 = vendslot1.getAmount() * s1.getSnackPrice();
	double valueVend2 = vendslot2.getAmount() * s2.getSnackPrice(); 
	double valueVend3 = vendslot3.getAmount() * s3.getSnack3Price();
	double valueVend4 = vendslot4.getAmount() * s4.getSnack4Price();
	double sum = valueVend1 + valueVend2 + valueVend3 + valueVend4;
	return sum;

} 

/*void miniVend::buySnack(int vendChoice)
{
	 
	 
	if ( vendChoice == 0)
	{
		  vdecrementAmount;
		  
		 
	}
	else if (vendChoice == 1)
	{
		vendslot2.decrementAmount;

	}
	else if (vendChoice == 2)
	{
		 &vendslot3.decrementAmount;

	}
	else if (vendChoice == 3)
	{
		 &vendslot4.decrementAmount;

	}

} */


Main file for testing
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
#include <iostream>
#include <string>
#include "Snack.h"
#include "VendSlot.h"
#include "miniVend.h"


using std::cout;
using std::cin;
using std::string;
using std::endl;

int main()
{
	Snack s1("corn chips", 0.75, 200);
	Snack s2("candy bar", 1.25, 300);
	Snack s3("root beer", 2.00, 450);

	VendSlot vs1(s1, 2);
	VendSlot vs2(s2, 1);
	VendSlot vs3(s3, 0);
	VendSlot vs4;  // five bottles of water

	miniVend machine(vs1, vs2, vs3, vs4, 0);


	
	cout << machine.numEmptySlots() << endl;
	cout << machine.valueOfSnacks() << endl;
	cout << machine.getMoney() << endl;
//	machine.buySnack(1);
	cout << machine.numEmptySlots() << endl;
	cout << machine.valueOfSnacks() << endl;
	cout << machine.getMoney() << endl;

	return 0;


}
minivend.h
----------
line 2: You need #define MINIVEND for your include guard to work properly.

minivend.cpp
------------
Line 11: You're creating a local variable which goes out of scope when the constructor exits. You're not initializing the class member variable.

Line 18-21: Again you're creating local variables that go out of scope when the constructor exist. You haven't shown vendslot.h, so it's not clear if snackprice is an attribute of a slot. Additionally, you not initializing moneyinmachine in this constructor.

main.cpp
--------
Lines 15-17, 19-21: Since you have not provided snack.h or VendSlot.h, it's not clear what the arguments are to these constructors.

Line 22: This appears to be an empty slot, contrary to the comment.

I am getting an error on my ValueOfSnacks functions stating s1 - s4 weren't declared

Where do think these are defined? Shouldn't getSnackPrice() be a member function of VendSlot? You load snacks into a slot. Therefore, a slot knows what it is loaded with and indirectly the price of the snack in that slot.


Last edited on
Thanks for the response! My primary question revolves are outputting the snack price over into ValueOfSnacks.. What is the best way to do this if it's located all the way in the Snack object? I am in the miniVend how do I move it over? I am going to provide the remainder of my code for clarity.

VendSlot.cpp
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
#include "VendSlot.h"
#include "Snack.h"
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

VendSlot::VendSlot()    //default constructor
{
	numOfSnacks = 5;
}

VendSlot::VendSlot(Snack the_snacks, int quantity) //overload constructor 
{
	the_snacks = snacks;
	numOfSnacks = quantity;
}

int VendSlot::getAmount()
{
	return numOfSnacks;
}
/*********************************
*Description: decrease count in vendSlot by when it's purchased.
**********************************/
void VendSlot::decrementAmount(int numOfSnacks)
{
	numOfSnacks = -1;
}


Snack VendSlot::getSnack()
{
	return snacks;
}

VendSlot::~VendSlot()
{
}



VendSlot header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef VENDSLOT_CPP
#define VENDSLOT_CPP
#include "Snack.h"
#include <string>

class VendSlot
{
public:
	VendSlot(); //default constructor
	VendSlot(Snack, int);
	Snack getSnack(); //return snack
	int getAmount(); //get amount of snacks available
	void decrementAmount(int); //function to decrease storage in vending machine by 1.
	~VendSlot(); //destructor 


private:
	Snack snacks; //passes snack object
	int numOfSnacks; // number of snacks
};

#endif // !VENDSLOT_CPP


Snack header
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
#ifndef SNACK_CPP
#define SNACK_CPP
#include <string>
using std::string;

class Snack
{
private:
	string nameOfSnack;
	double snackPrice;
	int numOfCalories;

public:
	Snack(); //default constructor
	Snack(string name, double price, int cals); //overload constructor
	~Snack(); //destructor

			  //Accessor functions

	string getNameOfSnack(); //returns name of snack
	double getSnackPrice(); //returns the price of the snack
	int getNumOfCalories(); //returns number of calories of snack
};



#endif // !SNACK_CPP



Snack.cpp
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
#include "Snack.h"
#include <iostream>
#include <string>

using std::endl;
using std::string;
using std::cout;
using std::cin;

Snack::Snack() //default constructor
{
	nameOfSnack = "bottled water";
	snackPrice = 1.75;
	numOfCalories = 0;
}

Snack::Snack(string name, double price, int cals)
{
	nameOfSnack = name;
	snackPrice = price;
	numOfCalories = cals;

}

Snack::~Snack()
{

}

string Snack::getNameOfSnack()
{
	return nameOfSnack;
}

double Snack::getSnackPrice()
{
	return snackPrice;
}

int Snack::getNumOfCalories()
{
	return numOfCalories;
}

My primary question revolves are outputting the snack price over into ValueOfSnacks..

Think about how most snack vending machines work. Price is a function of the slot. The "price" of the slot is programmed into the machine. The machine doesn't care what the price of each snack in a slot is.

I would make price an attribute of a slot. When you "load" a snack into a slot copy over the price of the snack into the slot's price.

Have you learned arrays yet? Your slots and snacks are better suited to arrays (or better yet a std::vector) rather than individual variables.

vendslot.cpp
------------
Line 13: If the slot is empty (no snacks have been added), then numOfSnacks should be 0.

Line 29-32: You're setting the argument to -1. The argument is passed by value, so you're not changing anything. You want to decrement the member variable.

Topic archived. No new replies allowed.