vector/iterator program

This is a discussion on vector/iterator program within the Game Programming forums, part of the General Programming Boards category; I'm expecting a lilttel flank from Bubba on this, but don't worry Bubba I'm on my way to classes so ...

  1. #1
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67

    Post vector/iterator program

    I'm expecting a lilttel flank from Bubba on this, but don't worry Bubba I'm on my way to classes so I can make a purty menu . Well anyhow, this is a program that I made to simulate a shop/inventory program using vectors and iterators. I believe I am very close to completing the program but I can't get int n to increase so I can put an item in the next slot of vector<string> inventory. Here is all of my code, I will be using functions in the final version to make it neater. There is also an executable file so you don't have to compile it to see how currently runs for me.

    Code:
    //Item Viewer v1.0
    
    #include<iostream>
    #include<vector>
    #include<string>
    #include<stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    //Declaring Stuff
    
    string quit, items, shop, key;
    vector<string> inventory(10, "empty slot\n"), tshop;
    tshop.push_back("1. *20 Ether");
    tshop.push_back("2. *15 Potion");
    tshop.push_back("3. *10 Antidote");
    string input;
    int buy;
    int n = 0;
    
    vector<string>::iterator myIter;
    myIter = inventory.begin() + n;
    
    //Welcome Player
    system("cls");
    cout<<"Welcome to Item Viewer v1.0!"<<endl;
    cout<<"Keywords:  items, shop, and quit"<<endl;
    cout<<"\nTo see the keywords again, type key"<<endl;
    
    while(input != "quit")
    {
    
    //Player Input
    cout<<"Input: ";
    cin>>input;
    system("cls");
    
    //Ahhhh, the if statments  
    
            if(input == "key")
            {
            cout<<"Keywords:  items, shop, and quit\n"<<endl;
            system("pause");
            }
    
            if(input == "items")
            {
            system("cls");
            cout<<"--Inventory--\n"<<endl;
                    for(int x = 0; x < inventory.size(); x++)
                    {
                    cout<<inventory[x]<<endl;
                    }
                    system("pause");      
            }
            
            if(input == "shop")
            {
            system("cls");
            cout<<"--Shop--\n"<<endl;
                    for(int x = 0; x < tshop.size(); x++)
                    {
                    cout<<tshop[x]<<endl;
                    }
    
                    cout<<"\n\nShopkeeper: \"What would you like to buy?\""<<endl;
                    cout<<"buy: ";
                    cin>>buy;
                    
                    if(buy == 1)
                    {
                    *myIter = "Ether\n";
                    n += 1;
                    }
                    
                    if(buy == 2)
                    {
                    *myIter = "Potion\n";
                    n += 1;
                    }
                    
                    if(buy == 3)
                    {
                    *myIter = "Antidote\n";
                    n += 1;
                    }
                    
                    cout<<"\n\n";
                    system("pause");
                    
            }
        }
        
        if(input == "quit")
        {
        cout<<"\n\nGoodbye!\n\n"<<endl;
        system("pause");
        }
    
            return 0;
    }

  2. #2
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    shoot, not sure if the executable file got attached....darn....I thought i hit attach....drats....

  3. #3
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Hello Drake. From some observations it doesn't appear that the variable n is related to anything, thus it is just a counter that does nothing. I believe the following code may work:

    replace:
    Code:
    n += 1;
    with:
    Code:
    myIter++;

  4. #4
    lv.42 Berserker Drake's Avatar
    Join Date
    Jun 2005
    Posts
    67
    Thank you, now, how would I put the .exe file out for people to download?

Popular pages Recent additions subscribe to a feed

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