Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how do i make this inventory thing work? it uses lists but doesn't show the bits at the end.

+2 votes
asked Jan 18, 2023 by human (250 points)
#include <iostream>
#include <vector>

//please run it on online gdb

using namespace std;
int main() {
    
    short counter = 0;
    bool hi = false;
    string answer;
    string pointer;
    vector<string> inventory2; // what takes it from the inventory
    vector<string> inventory = {"map", "pc", "atlas", "compass"}; // the inventory
    cout << "welcome to my inventory thing.\n";
    cout << "in the inventory, there is a telescope, a map, an atlas, a";
    cout << " gaming pc, and a compass.\n";
    cout << "remove what you like.\n";
    getline(std::cin >> std::ws, answer);
    for(int i = 0; i<inventory.size(); i++) {
        
        if(inventory[i] == answer) {
            
            hi = true;
            
        }
        
    }
    if(hi == true) {
        
    } else {
        
        while(hi != true) {
            cout << "enter a thing in the inventory.\n";
            getline(std::cin >> std::ws, answer);
            for(int i = 0; i<inventory.size(); i++) {
                
                if(answer == inventory[i]) {
                    
                    hi = true;
                    
                }
                
            }
            
        }
        
    }
    cout << "removing " << answer << " from inventory.\n";
    for(int i = 0; i < inventory.size(); i++) {
        
        if(answer == inventory[i]) {
            for(int i = 0; i<inventory.size(); i++) {
                
                if(inventory[i] == answer) {
                    
                    counter = i;
                    
                }
                
            }
            for(int i = inventory.size(); i > counter; i++) {
                
                if(i > counter) {
                    
                    inventory2.push_back(inventory[i]);
                    inventory.pop_back();
                    
                }
                if(i == counter) {
                    
                    pointer = inventory[i];
                    inventory.pop_back();
                    
                }
                
            }
            
        }
        
    }
    cout << "successfully removed " << pointer << " in inventory.\n";
            cout << "now there is :\n";
            for(int i = 0; i < inventory.size(); i++) {
                
                cout << inventory[i] << "\n";
                
            }
            cout << "\n that is everything in the inventory\n";
    
    return 0;
}

1 Answer

0 votes
answered Jan 18, 2023 by Peter Minarik (86,040 points)

Your logic is wrong

  1. when you copy elements from inventory to inventory2, except the one the user picked.
  2. when you print the final result as you print the old inventory, not the new inventory2.
To fix #1, I'd do something like this:
    for(int i = 0; i < inventory.size(); i++) {
        if (answer != inventory[i]) {
            inventory2.push_back(inventory[i]);
        }
    }
    inventory.clear();

By the way, your whole code is overcomplicated. You should make use of the C++ standard library functions.

Here's how I'd rewrite it:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> inventory = { "map", "pc", "atlas", "compass" };

void PrintContent(const std::string & message)
{
    std::cout << message;
    for (size_t i = 0; i < inventory.size(); i++)
    {
        if (i > 0)
            std::cout << ',';
        std::cout << ' ' << inventory[i];
    }
    std::cout << std::endl;
}

int main() {
    // Welcome
    std::cout << "Welcome to my inventory." << std::endl;
    PrintContent("In the inventory, there is:");

    // Get the item to be removed.
    std::vector<std::string>::iterator itemPosition;
    std::string itemToBeRemoved;
    do
    {
        std::cout << "What would you like to remove? ";
        getline(std::cin >> std::ws, itemToBeRemoved);
        itemPosition = std::find(inventory.begin(), inventory.end(), itemToBeRemoved);
    } while (itemPosition == inventory.end());

    // Removing the item
    std::cout << "Removing " << itemToBeRemoved << " from inventory..." << std::endl;
    inventory.erase(itemPosition);
    PrintContent("The new content of the inventory is:");

    return 0;
}
commented Jan 19, 2023 by human (250 points)
thanks so much (:
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...