0

Im trying to make a basic inventory management c++ program, what im trying to do is to add specific value (string) into array inventory (string too) here's the code:

#include <string>
class OurInventory{
    private:
        int size;
        std::string inventory;
    public: 
        void createArr(){
            std::cin >> size;
            std::string* inventory = new std::string[size];
            std::cout << "Created array with size of " << size << std::endl;
        }
        void addItem(){
            int userInputIndex;
            std::cin >> userInputIndex;
            if(userInputIndex >= size){
                std::cout << "Wrong value!";

            }
            else{
                std::string userInputData;
                std::cin >> userInputData;
                inventory[userInputIndex] = userInputData;
            }

        }
};
int main(){
    OurInventory obj;
    obj.createArr();
    obj.addItem();
}

So i tried declaring userInputData as an array of characters and i didnt worked too. I was excepting a prompt that asks me for specific index in the array and also a prompt that asked for what value to put in that index. It gave me this error:

error: cannot convert 'std::string' {aka 'std::__cxx11::basic_string<char>'} to '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'} in assignment
   25 |                 inventory[userInputIndex] = userInputData;
      |                                       ^~~~~~~~~~~~~
      |                                       |
      |                                       std::string {aka std::__cxx11::basic_string<char>}
sweader
  • 13
  • 4
  • 1
    "what im trying to do is to add specific value (string) into array inventory" - But the member `inventory` is not an array. It's a single `std::string` – TheUndeadFish Mar 29 '23 at 16:04
  • 5
    The local `inventory` in `createArr` is not the member with the same name. – wohlstad Mar 29 '23 at 16:05
  • 1
    Btw there is no `inv[userInputIndex]` in your code while your error says there is. – Jason Mar 29 '23 at 16:05
  • It is true that you can not store an entire `std::string` in a single `char`. What are you trying to do there? – Drew Dormann Mar 29 '23 at 16:05
  • sorry i putted the wrong error, there is inventory[userInputIndex] = userInputData; i copied the wrong error, i tried adding inventory into another variable inv before, otherwise its all the same – sweader Mar 29 '23 at 16:08
  • `std::string inventory;` -- This is a single string, not an "array". This should be `std::vector inventory;` if you want to learn C++ as it stands now and has stood for the past 25 years, and not back in the early 1990's. And then `inventory.resize(size);` inside of `createArr()` instead of what you're doing now. – PaulMcKenzie Mar 29 '23 at 16:11
  • You are overshadowing field by local variable: `std::string* inventory = new std::string[size];`. And type of field is wrong. Do not use `new` explicitly, in this case use `std::vector`! – Marek R Mar 29 '23 at 16:11
  • Hint: don't use raw pointers like this `std::string* inventory = new std::string[size];`, it's a very bad idea, unless your teacher/tutor/professor etc. explicitly tells you to do so, but even then it's a bad idea. It's so much simpler with a `std::vector`. – Jabberwocky Mar 29 '23 at 16:39
  • The `inventory` variable inside `createArray()` will disappear after execution leaves the function because you are declaring a local variable. Because it will disappear, the location of the dynamic memory will disappear too; this is known as a *memory leak*. – Thomas Matthews Mar 29 '23 at 16:50

1 Answers1

2

You redeclare inventory in your createArr() function, with a different type to boot. In C++ if you add a type before a parameter you (re)declare it for the current scope. The fix becomes:

#include <string>
class OurInventory{
    private:
        int size;
        std::string * inventory;
    public: 
        void createArr(){
            std::cin >> size;
            inventory = new std::string[size];
            std::cout << "Created array with size of " << size << std::endl;
        }
        void addItem(){
            int userInputIndex;
            std::cin >> userInputIndex;
            if(userInputIndex >= size){
                std::cout << "Wrong value!";

            }
            else{
                std::string userInputData;
                std::cin >> userInputData;
                inventory[userInputIndex] = userInputData;
            }

        }
};
int main(){
    OurInventory obj;
    obj.createArr();
    obj.addItem();
}

By removing the string* before the inventory in the createArr() function, I make sure it uses inventory the member variable.

Some other recommendations:

  • You have no destructor of your class, so the memory you allocate to the inventory pointer will never be released and result in a memory leak.
  • Look into std::vector for creating an array of items. This has better memory management and has additional convenience functions added to it.
oddstar
  • 502
  • 3
  • 12
  • The OP should research: [https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) because as soon as a destructor is added to correct the memory leak you will have to be very careful that your class does not violate the rule of 3. – drescherjm Mar 29 '23 at 16:15