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
----------
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.
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.
#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
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.