using cin with structs

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?


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
int main(int argc, const char * argv[]) {
    using namespace std;
    
    
    
        double money = 15;
    
    
        struct product{
            string name;
            double price;
        };
    
    
    
        product A31;
        A31.name = "Mountain Dew";
        A31.price = 2.99;
    
        product B32;
        B32.name = "Coke";
        B32.price = 3.99;
    
        product C33;
        C33.name = "Dr. Bob";
        C33.price = 9.99;
    
        product D34;
        D34.name = "Diet Coke";
        D34.price = 3.99;
    
        product E35;
        E35.name = "Pepsi";
        E35.price = 3.99;
    
        product F36;
        F36.name = "Weed Cola";
        F36.price = 1.99;
    
        product A21;
        A21.name = "Mountain Dew Baja Blast";
        A21.price = 2.99;
    
        product B22;
        B22.name = "Myspac drink";
        B22.price = 0.99;
    
        product C23;
        C23.name = "Bottled Water";
        C23.price = 0.99;
    
        product D24;
        D24.name = "Talabrew";
        D24.price = 5.99;
    
        product E25;
        E25.name = "Mug Rootbeer";
        E25.price = 1.99;
    
        product F26;
        F26.name = "Dr.Perky";
        F26.price = 2.99;
    
        cout << "Welcome to the Froggy Vendor. What would you like to purchase? (You have $" << money << " Currently in your balance.)";
    getline(cin,product);


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"
Last edited on
Not really tested....
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
#include <iostream>
#include <string>

int main()
{
   struct product
   {
      std::string name;
      double      price;
   };

   product A31;
   A31.name = "Mountain Dew";
   A31.price = 2.99;

   product B32 { "Coke", 3.99 };

   product C33 { "Dr. Bob", 9.99 };

   product D34 { "Diet Coke", 3.99 };

   product E35 { "Pepsi", 3.99 };

   product F36 { "Weed Cola", 1.99 };

   product A21 { "Mountain Dew Baja Blast", 2.99 };

   product B22 { "Myspac drink", 0.99 };

   product C23 { "Bottled Water", 0.99 };

   product D24 { "Talabrew", 5.99 };

   product E25 { "Mug Rootbeer", 1.99 };

   product F26 { "Dr.Perky", 2.99 };

   double money { 15 };

   std::cout << "Welcome to the Froggy Vendor. What would you like to purchase? (You have $" << money << " Currently in your balance.)\n";
   product input;
   std::getline(std::cin, input.name);

   std::cout << "You ordered a " << input.name << '\n';
}
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.
Last edited on
Topic archived. No new replies allowed.