how can i make a buying system with iterator c++? | Sololearn: Learn to code for FREE!
0

how can i make a buying system with iterator c++?

item lBoots("armor","Leather Boots",40,10); std::vector<item>inventory; std::vector<item>::iterator iter; void buyItem(player &p,item &a) { cout << p.hp; for(iter=inventory.begin(); iter != inventory.end(); iter++) { if(iter->name==a.name) { if(a.own=true) { cout << "You already have that item."; break; } else { p.gold-=a.value; a.own=true; if(a.type=="weapon") { p.damage+=a.feature; } if(a.type=="armor") { p.maxHp+=a.feature; } cout << p.hp; break; } break; } } } i have theese so far but the func is not working what should i do

11th Apr 2017, 11:59 AM
Furkan Kabak
Furkan Kabak - avatar
5 Answers
+ 1
Assuming you are iterating through the player's inventory, you should check if an item's name matches the item you want to buy, so this is already your check whether the player has the item or not, so if this is true, just tell the player he's blind and return from the function. When the iterator reaches the end you can say that the player does not have the item and only then should you attempt to add the item. Also you should either use std:: consistently or not at all std::vector and std::cout or using namespace std; and vector and cout, etc. I don't know the rest of the program but this kinda looks like you're putting the inventory of the player in global space, just put it in the player's class ^^
11th Apr 2017, 12:46 PM
Dennis
Dennis - avatar
+ 1
https://code.sololearn.com/cgEiXlPGXf3j/?ref=app I made some code, it might not be perfect but it may give an idea. Lemme know if you have questions.
11th Apr 2017, 1:56 PM
Dennis
Dennis - avatar
0
i have a shop function either it has ability to check money:D if enough buyItem(p,a) works like this but this func doesnt work also if i dont use if(a.own==true) how can i understand the player has that item or not
11th Apr 2017, 12:26 PM
Furkan Kabak
Furkan Kabak - avatar
0
so you know about vectors as u see i created inventory before main() also i want to use inventory.push_back() but the compiler gives error that inventory doesnt name a type what should i do
11th Apr 2017, 12:58 PM
Furkan Kabak
Furkan Kabak - avatar
0
i got ur point i will try this tomorrow i will come back tomorrow if i have any questions thank you
11th Apr 2017, 1:48 PM
Furkan Kabak
Furkan Kabak - avatar