I'm trying to create a digital vending machine using structs for all of the Items in the vending machine, but I just tried using cin and im getting an error that "product does not refer to a value", do I have to write cins with structs in a specific way? Will I have to use arrays?
Edit, doing some more research and I **think** I have to ask the user to input one of the variables inside the struct? So I'm going to try and create a variable called "tag" and have the name be the same as each product I'm "selling"
Welcome to the Froggy Vendor. What would you like to purchase? (You have $15 Currently in your balance.)
Pepsi
You ordered a Pepsi
Maintaining your "inventory" using separate variables will make it harder than it should be to compare what the user ordered to the available items. Using a container like a vector would allow for looping to compare each item with the input.
Yes you either need to ask the user to input each variable in the structure or you could overload istream operator>> to ask the user for each variable in the structure.
you can overload the << and >> operators for cin and cout making them friends of istream and ostream to allow cin and cout to work with your object.
this is sometimes useful, and sometimes just doing as you said and reading each field one by one manually is the way to go.
If you need to see an example, though, search the web around "overload >> for cin c++" and you will get many examples of how to do this. Its intimidating if you are new to the idea, but the concept is simple, you are providing a "what to do" function for the compiler to use when it is asked to cin or cout your new type (which it has never seen before and does not know what you want to do to it). The language COULD have just dumped everything any old way on the screen with a default overload for all new objects, but that is almost never what anyone wants -- guessing the programmer's use case rarely works -- so they left it up to you to do for yourself.
you can also provide a *cast operator* (another google adventure) of your object so that it can be cast to a string, which you can then inject into cout (it won't help cin at all, and I really cannot imagine going to the trouble of adding a string to your object conversion, though its possible). This is more useful in GUI programming for the same idea but UI tools almost always wants you to pass around strings instead of using stream operations. The cast operations are picky and often require the actual cast manually, eg cout << (string) mything. cout << mything may still fail depending on the alignment of the stars whether it figures out the implict casting or no.