Assign values to vector elements

Ok so i made a program that asks for a number of products you want to sell and it asks for a price but i dont think im assigning the price to the vector elements, how do i do that? so lets say product 1 is an zbox, and i set the price to $400, later in my program when i buy an xbox i want the program to know to subtract $400.

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 <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    string vecs;
    int num = 0;
    int price = 0;

    vector<string> svector;
    vector<int> ivector;

    cout << "How many products do you want to sell?" << endl;
    cin >> num;

    cin.ignore(80, '\n');
    cout << "\n";

    for(int i = 0; i < num; i++)
    {
        cout << "Product " << i << " ";
        getline(cin, vecs);
        svector.push_back(vecs);
        cout << "Ok now enter the price for this $";
        cin >> price;
        ivector.push_back(price);
        cout << "\n";
        cin.sync();
    }

    cout << "\n";
    for(int i = 0; i < num; i++)
    {
        cout << svector[i] << " $" << ivector[i] << endl;
    }
}
i dont think im assigning the price to the vector elements
You are.
I meant im not assigning the int vector elements to the string vector elements.
Nope. You are.
Why do you think you're not?
Because there 2 different vectors how does it know to assign the int vector to the string vector? this is what i have so far and its not working. It compiles but doesnt subtract the amount i entered for the item i want.

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
#include <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    string vecs;
    int buy;
    int num = 0;
    int price = 0;
    int money = 5000;

    vector<string> svector;
    vector<int> ivector;


    cout << "How many products do you want to sell?" << endl;
    cin >> num;

    cin.ignore(80, '\n');
    cout << "\n";


    for(int i = 0; i < num; i++)
    {
        cout << "Product " << i << " ";
        getline(cin, vecs);
        svector.push_back(vecs);
        cout << "Ok now enter the price for this $";
        cin >> price;
        ivector.push_back(price);
        cout << "\n";
        cin.sync();
    }

    cout << "\n";

    for(int i = 0; i < num; i++)
    {
        cout << svector[i] << " $" << ivector[i] << endl;
    }

    cin.sync();
    cout << "Would you like to buy something?" << endl;
    cout << "Money: $" << money << endl;
    for(int i = 0; i < num; i++)
    {
        cout << i << " " << svector[i] << endl;
        cin >> buy;

        if(buy == i)
        {
            ivector[i] -= money;

            cout << "current Money: " << money << endl;
        }
    }
}
Last edited on
Oh! Sorry, my bad. I misunderstood what you meant.

If I understand what your code is really meant to be doing, what you're doing up to line 49 is correct (or, at least, can be made to work depending on what you write next). Take a good, hard look at that loop and see if you can find what's wrong
Ok i've looked at it and i honestly cant figure it out. What do i do?

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
#include <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
    string vecs;
    int buy;
    int num = 0;
    int price = 0;
    int money = 5000;

    vector<string> svector;
    vector<int> ivector;


    cout << "How many products do you want to sell?" << endl;
    cin >> num;

    cin.ignore(80, '\n');
    cout << "\n";


    for(int i = 0; i < num; i++)
    {
        cout << "Product " << i << " ";
        getline(cin, vecs);
        svector.push_back(vecs);
        cout << "Ok now enter the price for this $";
        cin >> price;
        ivector.push_back(price);
        cout << "\n";
        cin.sync();
    }

    cout << "\n";

    for(int i = 0; i < num; i++)
    {
        cout << i << " " << svector[i] << " $" << ivector[i] << endl;
    }

    cout << "\n";
    cout << "Would you like to buy something?" << endl;
    cout << "Current money: $" << money << endl;
    cin >> buy;

    for(int i = 0; i < num; i++)
    {
        if(buy == i)
        {
            num -= money;
            cout << "current Money: " << money << endl;
            cin.get();
        }
    }
}


Am i closer? what else do i need to do?
Last edited on
I feel like im really close, it all compiles and i can select an option it just isnt subtracting the money
Never mind i finally got i holy shit balls im stupid. i neede to do money -= ivector[i]; :P
Topic archived. No new replies allowed.