Matching one part of a struct with another

follow-up to https://www.cplusplus.com/forum/beginner/282933/
Thanks for your help on my last post, but I've already gotten stuck again, I need to know if its possible to input one variable of a struct and have them output another variable of said struct (e.g: User inputs item tag "A31" and the program outputs "Mountain Dew"), I think this is possible with if statements but that would make the code extremely long and It wouldn't work with how the program is intended to be used. Sorry for coming here so much for help but I really need the 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
#include <iostream>

int main(int argc, const char * argv[]) {
    using namespace std;
    
    
 
    double money = 15;
    
            struct product{
            string tag;
            string name;
            double price;
        };
    
    
    
        product A31;
        A31.tag = "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.)";
    product input;
    getline(cin, input.tag);
    
    cout << "You ordered a " << input.name << "\n";
Last edited on
As was pointed out in the other thread, you need to use a vector
which allows you to easily index through your products.
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>
#include <vector>

int main()
{
    struct product
    {
        std::string tag;
        std::string name;
        double      price;
        //  Constructor
        product(std::string _tag, std::string _name, double _price)
        {
            tag = _tag;
            name = _name;
            price = _price;
        }
    };
    std::vector<product> products 
    { {"A31", "Mountain Dew", 2.99},
        {"B32", "Coke", 3.99},
        {"C33", "Dr. Bob", 9.99},
        {"D34", "Diet Coke", 3.99},
        {"E35", "Pepsi", 3.99},
        {"F36", "Weed Cola", 1.99},
        {"A21", "Mountain Dew Baja Blast", 2.99},
        {"B22", "Myspac drink", 0.99},
        {"C23", "Bottled Water", 0.99},
        {"D24", "Talabrew", 5.99},
        {"E25", "Mug Rootbeer", 1.99},
        {"F26", "Dr.Perky", 2.99 } };

    double money{ 15 };
    std::string input;

    std::cout << "Welcome to the Froggy Vendor. What would you like to purchase? (You have $" << money << " Currently in your balance.)\n";
    std::cin >> input; 
    for (int i=0; i<products.size(); i++)
        if (products[i].tag == input)
        {
            std::cout << "You ordered a " << products[i].name << '\n';
            break;
        }
}
Thanks, I sortof skimmed through the text after reading the code which is my fault. Will try
that aside, this is a major POINT of structs/objects, is the ability to make a relationship between the fields. That is what it DOES, at the lowest possible level of starting to think about objects.
just saying
struct foo
{
int x;
string s;
};
relates x and s. the value in s belongs with x, and x belongs with s. They may not have a fixed relation ship, eg x could be how many s'es you bought, so it could be 8 now and 10 later, or they could have a hard relationship, like x is an ID number that means "s" in english.
The code has to use the relationship appropriately for whatever it means. And, I suppose, the two values can be somewhat unrelated, x could be the number of people in india and s could be the gettysburg address, but even in that idiotic case the two still have a relationship just by being structed together that the programmer is responsible for making sense of somehow (if no sense can be made, they should not have been tied together, possible but not a good case to discuss as it is a flawed design or implementation). The relationship could be as simple as "some function needed these 2 things".
Last edited on
Topic archived. No new replies allowed.