Need help with this vector and struct

closed account (EAp4z8AR)
Now the issue I keep receiving is:
‘class std::vector<item>’ has no member named ‘item’
if (name = vendingMachine.item)

1
2
3
4
5
6
 #include <iostream>
#include <math.h>
#include <iomanip>
#include <vector>

using namespace std;



Cant change this entire section:
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
struct item
{
    string name; // name of the item
    int code; // numerical code entered to select the item
    float price; // price per unit of the item
    int stock; // number of items available in the machine
};

void stock(string name, int count, vector<item> &vendingMachine);

int main()
{
    vector<item> vendingMachine; //This right here is what I am confused about because my IDE keeps saying that "item" is not declared
    stock("Doublemint Gum", 1, vendingMachine);
    cout << "-------------------" << endl;
    purchase(.50, 100, vendingMachine); //Don't worry about this part to the bottom yet
    cout << "-------------------" << endl;
    stock("Chocolate Chip Cookies", 10, vendingMachine);
    cout << "-------------------" << endl;
    stock("Chocolate Chip Cookies", 5, vendingMachine);
    cout << "-------------------" << endl;
    purchase(.75, 100, vendingMachine);
    cout << "-------------------" << endl;
    purchase(1.00, 100, vendingMachine);
    cout << "This vending machine now has $" totalValue(vendingMachine) <<
    fixed << setprecision(2) << " worth of items" << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void stock(string name, int count, vector<item> &vendingMachine)
{
    if ( ) //Check if item already exists and if it does then add to the stack
    {
        vendingMachine.stock += count;
    }
    
    else //Create new item
    {
        cout << "The item you are adding, " << name << ", was not in the machine.\nPlease enter the following information for this item." << endl;
        item newItem;
        newItem.name = name;
        newItem.price = .75;
        newItem.stock = 5;
        newItem.code = 100;
        vendingMachine.push_back(newItem);
        
    }
}
Last edited on
closed account (EAp4z8AR)
the output should be:

The item you are adding, Doublemint Gum, was not in the machine.
Please enter the following information for this item
Code: 100
Price: $0.75
You have added 1 units of Doublemint Gum. Now there are 1 units in the
machine
item must be defined before stock can be declared.
closed account (EAp4z8AR)
Ahh got you thank you.
It's conventional to use a typedef to "simplify" declarations.
1
2
3
4
5
6
7
8
struct Item
{
    string name; // name of the item
    int code; // numerical code entered to select the item
    float price; // price per unit of the item
    int stock; // number of items available in the machine
};
typedef std::vector<Item> Items;


Then main becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    Items vendingMachine; //This right here is what I am confused about because my IDE keeps saying that "item" is not declared
    stock("Doublemint Gum", 1, vendingMachine);
    cout << "-------------------" << endl;
    purchase(.50, 100, vendingMachine); //Don't worry about this part to the bottom yet
    cout << "-------------------" << endl;
    stock("Chocolate Chip Cookies", 10, vendingMachine);
    cout << "-------------------" << endl;
    stock("Chocolate Chip Cookies", 5, vendingMachine);
    cout << "-------------------" << endl;
    purchase(.75, 100, vendingMachine);
    cout << "-------------------" << endl;
    purchase(1.00, 100, vendingMachine);
    cout << "This vending machine now has $" totalValue(vendingMachine) <<
    fixed << setprecision(2) << " worth of items" << endl;
}


An now we can focus on stock.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void stock(std::string name, int count, Items &vendingMachine)
{
    auto pItem = std::find_if(std::begin(vendingMachine), std::end(vendingMachine), [&name](const Items& item) { return item.name == name; });
    if (pItem != std::end(vendingMachine)
    {
        vendingMachine->stock += count;
    }
    else //Create new item
    {
        cout << "The item you are adding, " << name << ", was not in the machine.\nPlease enter the following information for this item." << endl;
        item newItem;
        newItem.name = name;
        newItem.price = .75;
        newItem.stock = 5;
        newItem.code = 100;
        vendingMachine.push_back(newItem);   
    }
}
closed account (EAp4z8AR)
Wait but I can not change the main function.
It was just a tip to keep things tidy. It might help in the future.

Edit: For what it's worth, I also don't really see it as helpful in this situation to cover up the fact that it's an std::vector. In other situations I definitely love to use typedefs/usings.
Last edited on
kbw wrote:
It's conventional to use a typedef to "simplify" declarations.

I am curious about the advantage of this convention. Doesn't that decrease code readability by obscuring type information? Or does the IDE usually tell you the type when you hover anyway?

I can see how it might be extremely useful. Oftentimes I start with something like vector <foo> foos and type it out 20 times in five files just to realize a list is more appropriate, then I have to go back and change 20 lines of code in five different files. With the typedef I would only have to change one line.

I suppose with very long type declarations it can save space, too.

std::unordered_multimap <unsigned long long, std::vector <unsigned long long>::iterator> longDeclaration is ugly.
Last edited on
Doesn't that decrease code readability by obscuring type information?
I think it increases code readability if the names are chosen well. For example: http://www.cplusplus.com/forum/beginner/245674/#msg1086855
Would you rather see:
 
void show_values(const vec2d_type& values)

or:
 
void show_values(const std::vector<std::vector<int>>& values)


Also, typedefs provide a layer of indirection. If you decide to change the type in some way, you change it once in the typedef, rather than looking for all instances throughout your code base.
A vector gives certain guarantees. If you change it to some other container you might break code that was relying on it being a vector.
closed account (EAp4z8AR)
Alright, I tried it using the the typedef function and see why it is probably much easier. But I still have to keep some of the things strictly how they are. I need help with getting the contents of the vendingMachine vector when I am checking in the stock function. Honestly it just isn't working like a regular vector.
Last edited on
what sort of help?
presumably you now have this

main
...
stock(name, counter, foovec);

...//then after that you can use foovec:

eg cout << foovec[item].something << endl;
I need help with getting the contents of the vendingMachine vector when I am checking in the stock function. Honestly it just isn't working like a regular vector.
It's just guess work on our part unless we can see your code.
Topic archived. No new replies allowed.