Integration>

I'm trying to create a "inventory selection" type of thing, but I'm having trouble integrating the functions for each of the separate packs without problems. The functions are left empty on purpose.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  # include <iostream>
# include <string>
# include <vector>

using namespace std;

string intro();
string selection();
string huntingPack();

int main()
{
    intro();
    
}

// the introduction part of the program ties to everything else so far.
string intro()
{
    string answer;
    
    cout << "Would you like to begin, agent?<y/n>" << endl;
    cin >> answer;
    
    if (answer == "y")
    {
        selection();
    }
    return 0;
}


// creating a seperate function for the selection part of the program
string selection()
{
    string selection;
    cout << " this is the selection of backpacks you can use." << endl;
    
    vector<string>inventory;
    inventory.push_back("hunting pack");
    inventory.push_back("electronics pack");
    inventory.push_back("casual wear pack");
    inventory.push_back("sling pack");
    
    vector<string>::iterator selec1;
    vector<string>::const_iterator iter;
    
    for(iter = inventory.begin(); iter != inventory.end(); ++iter)
    {
        cout << *iter << endl;
    }
    
    cout << " select a pack (enter 'help' for more info)" << endl;
    cin >> selection;
    
    if(selection == )
    {
        huntingPack();
    }
    
    return 0;
    
    if(selection == "Help")
    {
        cout << "[ Making a selection]" << endl;
        
    }
    
}

// making an array for the attributes of the pack, as well as the picking stuff out part.

string huntingPack()
{
    
    
    
}

string electronicsPack()
{
   
    
    
    
}
You can use a std::map
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
#include <functional>
#include <map>


string selection()
{
    string selection;
    cout << " this is the selection of backpacks you can use." << endl;
    std::map<string, std::function<string(void)>> inventory;
    inventory["hunting pack"] = huntingPack();
    inventory["electronics pack"] = electronicsPack();
    inventory["casual wear pack"] = casualPack();
    inventory["sling pack"] = slingPack();

    for(auto &pair : inventory) //Use the C++11 for-each loop
    {
        cout << pair.first << endl;
    }

    cout << " select a pack (enter 'help' for more info)" << endl;
    cin >> selection;
    auto result = inventory.find(selection);
    cout << selection;
    if(result != inventory.end())
    {
        string pack_return = result->second(); //call the associated function
    }
    else
    {
        cout << "That's not a valid pack!" << endl;
    }

    return "";

    if(selection == "Help")
    {
        cout << "[ Making a selection]" << endl;

    }

}


See
http://en.cppreference.com/w/cpp/container/map
http://en.cppreference.com/w/cpp/utility/functional/function
Last edited on
Topic archived. No new replies allowed.