Vector help again

Ok so i have this code and i want to know how to show pre defined items in the vector so when i call it, it will show all of them in a list and the player can choose from the list what they want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;
    string Vitem1 = "pop";

    vector<string> strVector;
    vector<int> intVector;

    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;
    cin >> input;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;
    string Vitem1 = "pop";

    vector<string> strVector;
    vector<int> intVector;
    vector<string>::const_iterator iter;
    for(iter = strVector.begin(); iter != strVector.end(); ++iter) 
           cout << *iter << std::endl;

    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;
    cin >> input;

}


Something like that? This will output the contents of 'strVector'.
if your compiler supports the range-based for statement you can output elements of the vector the following way


for ( auto s : strVector ) std::cout << s << std::endl;
No, i want items to already show up in the vector, so lets say i want 5 items, pop, candy bar, chips, juice, Chocolate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;
    string Vitem1 = "pop";

    vector<string> strVector;
    vector<int> intVector;
    strVector.push_back("Pop");
    strVector.push_back("Candy Bar");
    strVector.push_back("Chips");
    vector<string>::const_iterator iter;
    for(iter = strVector.begin(); iter != strVector.end(); ++iter) 
           cout << *iter << std::endl;

    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;
    cin >> input;

}


Like that?
yeah, what is vector<string>::const_iterator
It defines an iterator for a string type vector...

It allows you to step through a vector instead of using at's, and controls the point in the vector that is currently being manipulated...

you could achieve similar (albiet slower iirc) with:
1
2
for(int i = 0; i < strVector.size(); ++i)
  cout << strVector.at(i) << std::endl;
It defines an iterator for a string type vector...

It allows you to step through a vector instead of using at's, and controls the point in the vector that is currently being manipulated...

Adding to that; const_iterator ensures none of the elements inside the vector will be modified when accessed by that particular iterator.
Last edited on
Ok now i need to be able to get the name of the item in the vector and use it in my case statement how do i do that? the way i have it, it just gives me a number

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
void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;

    vector<string> strVector;
    vector<int> intVector;
    strVector.push_back("Pop");
    strVector.push_back("Chips");
    strVector.push_back("Candy Bar");
    strVector.push_back("poop");

    for(int i = 0; i < strVector.size(); ++i)
    {
         cout << i << " " << strVector.at(i) << endl;
    }

    cout << "\n";
    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;

    cin >> input;

    switch(input)
    {
        case 0:
            cout << "Thank you for purchasing" << strVector.size() << endl;
            break;
        case 1:
            cout << "choice 1" << endl;
            break;
        case 2:
            cout << "choice 2" << endl;
            break;
        case 3:
            cout << "choice 3" << endl;
            break;
    }
}
Last edited on
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 <iostream>
#include <string>
#include <vector>

int main(void) {
    string str;
    int input;
    int rem;
    int money = 5000;

    vector<string> strVector;
    vector<int> intVector;
    strVector.push_back("Pop");
    strVector.push_back("Chips");
    strVector.push_back("Candy Bar");
    strVector.push_back("poop");

    cout << "\n";
    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;

    cin >> input;

    switch(input)
    {
        case 0:
            cout << "Thank you for purchasing" << strVector.size() << endl;
            break;
        case 1:
            cout << strVector[input-1] << std::endl;
            break;
        case 2:
            cout << strVector[input-1]  << endl;
            break;
        case 3:
            cout << strVector[input-1]  << endl;
            break;
    }
    return 0;
}


You mean like that? It's not really the ideal way to get an element from the vector, but it will surely suffice.
This seems to be the easiest way to do it for something like this.
Last edited on
No that dosnt work it keeps giving me pop. is there any way i can get i from the for loop? so i can do strVector[i]
I suspect we're missing something here...

Can you put down exactly what you expect to be being output, and exactly what you are seeing? What are the inputs and outputs?

"cout << strVector[input-1] << std::endl;" will only output the contents of the element in the vector at (input-1). If you're expecting the element's position as well (aka being the 1st element) then you need to add code.

Just so we can be clear, the code directly above will do the following:

Create variables,
Populate the strVector with: Pop, Chips, Candy Bar, poop (in that order)
output:
""
"What do you want to buy?"
"Money $5000"
"Only Type the number to the left of the item"
Will wait for input (you havent told me the items yet)
On entering 0:
output "Thank you for purchasing 4"
on entering 1:
output "Pop"
on entering 2:
output "Chips"
on entering 3:
output "Candy Bar"

then terminate.


If you put in the for loop from your previous post, you'll have the list to select too.
Last edited on
well everything works fine its just in the switch statement i want it to show which item they bought.

cout << "Thank you for purchasing " << strVector[i]

I know i could just do cout << "thank you for purchasing pop" << endl;

but i want to do this because im trying to learn different ways of doing stuff.
if you want to output the name of the item that was selected, do as you have listed just above, you dont need to use a switch statement to achieve the direct above.

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
void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;

    vector<string> strVector;
    vector<int> intVector;
    strVector.push_back("Pop");
    strVector.push_back("Chips");
    strVector.push_back("Candy Bar");
    strVector.push_back("poop");

    for(int i = 0; i < strVector.size(); ++i)
    {
         cout << i << " " << strVector.at(i) << endl;
    }

    cout << "\n";
    cout << "What do you want to buy?" << endl;
    cout << "Money $" << money << endl;
    cout << "Only type the number to the left of the item" << endl;

    cin >> input;

    if(input < strVector.size()){
	cout << "Thank you for purchasing" << strVector[input-1] << endl;
    }
    
    return 0;
}


quick edit, to avoid entering a number greater than the vector space
Last edited on
It doestn work, it just says thank you for purchasing, and then my program crashes.
Thats correct, the code as you've written it so far (with the amendments by us) doesnt loop, you have no while statement here, and theres no reason to loop...

If the return 0; shouldnt be there, take it out, it might have gotten mixed up in one of the previous posts.
so i need a while loop? there was no return statement in the program when it crashed. it seems like its crashing because of strVector[input-1]
What should happen is it should display your options, you enter a number to select the option, and it should say thanks for purchasing <insert option name here> , then close.

if you want to run it over and over, you need to wrap it up in a while statement (roughtly the below):

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
void shop()
{
    string str;
    int input;
    int rem;
    int money = 5000;

    vector<string> strVector;
    vector<int> intVector;
    strVector.push_back("Pop");
    strVector.push_back("Chips");
    strVector.push_back("Candy Bar");
    strVector.push_back("poop");

    do{
	    cout << "Items available: \n";

	    for(int i = 0; i < strVector.size(); ++i)
	    {
	         cout << i << " " << strVector.at(i) << endl;
	    }
	
	    cout << "\n";
	    cout << "What do you want to buy (enter 0 to exit)?" << endl;
	    cout << "Money $" << money << endl;
	    cout << "Only type the number to the left of the item" << endl;
	
	    cin >> input;
	
	    if(input < strVector.size() && input != 0){
		cout << "Thank you for purchasing" << strVector[input-1] << endl;
	    }
    }while(input != 0);
    return 0;
}
Last edited on
Topic archived. No new replies allowed.