### 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include #include using namespace std; int main(void) { string vecs; int num = 0; int price = 0; vector svector; vector 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include using namespace std; int main(void) { string vecs; int buy; int num = 0; int price = 0; int money = 5000; vector svector; vector 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?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include using namespace std; int main(void) { string vecs; int buy; int num = 0; int price = 0; int money = 5000; vector svector; vector 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.