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;
}
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'.
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;
}
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
#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.
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.
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.
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.
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):