Another Logic Error

I once again have a logic error. This time I am positively baffled: I am making a function that messes with three vectors it receives as parameters. The function adds new items to those vectors, but the funny thing is, while it completely normally adds items to two of the three, it fails to do so for the first, despite the fact that I use the same template! Here the function is:

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
88
89
90
91
92
93
94
95
96
97
98
void giveReward( Character player, vector<string>& spells, vector<string>& weapons, vector<string>& armor )
{
    int reward, rewardGen;
    srand( time( 0 ) );
    reward = rand() % 3 + 1;

    cout << "\nYour reward: ";

        switch( reward )
        {
        case 1:
            if( binary_search( spells.begin(), spells.end(), "Ignite" ) )
            {
                cout << "2 new spells!\n\n";
                system( "pause" );
                spells.clear();
                spells.push_back( "Fireball" );
                spells.push_back( "Heal" );
                player.magicDamage += 2;
                player.magicHeal += 2;
            }
            else if ( binary_search( spells.begin(), spells.end(), "Fireball" ) )
            {
                cout << "2 new spells!\n\n";
                system( "pause" );
                spells[0] = "Incinerate";
                spells[1] = "Regenerate";
                player.magicDamage += 3;
                player.magicHeal += 3;
            }
            else if( binary_search( spells.begin(), spells.end(), "Incinerate" ) )
            {
                cout << "2 new spells!\n\n";
                system( "pause" );
                spells[0] = "Inferno";
                spells[1] = "Revive";
                player.magicDamage += 5;
                player.magicHeal += 5;
            }
            else
            {
                giveReward( player, spells, weapons, armor );
                return;
            }
            break;

        case 2:
            cout << "a new weapon!\n\n";
            system( "pause" );

            rewardGen = rand() % 5 + 1;

            switch( rewardGen )
            {
            case 1:
                weapons.push_back( "Sharp Steel Longsword" );
                break;
            case 2:
                weapons.push_back( "Heavy Long Steel Axe" );
                break;
            case 3:
                weapons.push_back( "Heavy Steel Shortsword" );
                break;
            case 4:
                weapons.push_back( "Long Steel Warstaff" );
                break;
            case 5:
                weapons.push_back( "Wooden Longbow" );
                break;
            }
            break;

        case 3:
            cout << "new armor!\n\n";
            system("pause");
            rewardGen = rand() % 5 + 1;

            switch( rewardGen )
            {
            case 1:
                armor.push_back( "Hardened Leather Jacket" );
                break;
            case 2:
                armor.push_back( "Heavy Steel Plate Armor" );
                break;
            case 3:
                armor.push_back( "Heavy Leather Padded Robe" );
                break;
            case 4:
                armor.push_back( "Chain Hauberk" );
                break;
            case 5:
                armor.push_back( "Light Steel Scale Vest" );
                break;
            }
            break;
        }
}

I have no idea what I am doing wrong, any advice will be appreciated!
Last edited on
In one case you are using push_back. in others you are replacing values at indices 0 and 1. Also I noticed, you are using binary_search. Note, that function works correctly only on sorted sequences! If your vector isn't sorted it probably just give you false every time. Who told you to use that useless function?
I can see how that would affect things, however, binary_search does not fail, as I am informed that my reward is two new spells. However, when I go back into combat and view my spells, it goes to the defaults I have set. In fact, the function apparently does not even alter magicDamage, as my spells still do the same damage...

However, what would a better function be for probing collections for specific elements?
I do not see what is the problem with your function. Maybe some other code alters vector of spells?
Also you are passing your player variable as copy: no changes to it will be visible outside function.
To search for specific item in unsorted container use std::find http://en.cppreference.com/w/cpp/algorithm/find
Or in your case you can simply check what first element is.
Can I pass classes in by reference? I am sorry, my mind still won't wrap around some of the theories behind classes...

Thanks for the function, though!
Can I pass classes in by reference?
Things to note: vector<string> is a class too.
Oh... okay... Thanks!
I tried various solutions, including passing the instance in by reference and passing the individual magicDamage and magicHeal variables in by reference, but the problem still persists! The thing is, this function works fine with my armor and weapons rewards, as in, the weapons and armor are all delivered, they show up in my inventory and all, but the spells simply won't appear, and the modifications to the damage and heal values also do not work...
I seem to have found the problem: the truth is, this function works fine, but all the values are lost. I have a visitVillage() global function which this will return to, and I have checked that. All the spells and modified values are fine there. However, when visitVillage() returns to play(), the body function, also global, not only are the spells and their associated values lost, but any alterations to the character are lost... is there some crazy trick I can use to preserve my values?
Are you passing player by reference?
Yes, I tried that, it did not work.

However, I managed to solve the problem using some saving and loading tricks (I save player information when he returns from his quest, where the information is preserved, and then I load it back up when the program returns to play()).

Thank you all for your input, though!
Topic archived. No new replies allowed.