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);
}
}
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
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);
}
}
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.
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.
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.
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.
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.