-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cpp
58 lines (48 loc) · 1.44 KB
/
Inventory.cpp
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
/************************************************************************
** Program Name: Space Rift, Final Project
** Author: Hillary Dreikorn
** Date: 11/22/2017
** Description: This is the implementation file for inventory class
************************************************************************/
#include "Inventory.hpp"
#include <iostream>
Inventory::Inventory(): name("No items")
{}
Inventory::Inventory(std::string n) : name(n)
{}
Inventory::~Inventory()
{}
//used to retried private data member name
std::string Inventory::get_name() {
return name;
}
//prints the contents of the inventory vector
void Inventory::print_inventory() {
std::cout << "Inventory :\n";
for (int i = 0; i < inventory.size(); i++) {
std::cout << inventory[i] << "\n";
}
}
//this will add the item by name to inventory vector
void Inventory::add_item(std::string item) {
inventory.push_back(item);
}
//this will search the vector for the string
//given as a parameter in the inventory vector
//it will return the number for the position found
//and wil return -1 if not found
//referenced from: Gaddis C++ Early Objects, Ch.9
int Inventory::search_inventory(std::string item) {
int index = 0;
int position = -1;
bool found = false;
int size = inventory.size();
while (index < size && !found) {
if (inventory[index] == item) {
found == true;
position = index;
}
index++;
}
return position;
}