#Gives error converting adding string into array of strings

8 messages · Page 1 of 1 (latest)

keen wind
#

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();
}```
Its excepted to give me a prompt which asks the specific index and what i want to add to that index.
What it gives me is this:
``` 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 |                 inv[userInputIndex] = userInputData;
      |                                       ^~~~~~~~~~~~~
      |                                       |
      |                                       std::string {aka std::__cxx11::basic_string<char>}```
iron valleyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

glossy parrot
#

It seems like you are confused on different things here

#
  • you have a string ‘inventory’ member, but you wanted an array (or pointer)
  • in createArr, you create a pointer to string ‘inventory’ — this variable is local and ‘hides’ the one of your class. Since it’s a local variable, you can’t use it outside this function
  • in createArr you are never deallocating the memory that you allocated
  • in addString, you are attempting to assign a string to ‘inventory’ (which you declared as a class member with type std:;string). That makes no sense, a string is an array of characters, not of strings
#

Hint1: use std::string* as your class member ‘inventory’, or better std::vectorstd::string or std::arraystd::string if you have a fixed size
Hint2: dont shadow your variables (lookup variable/name shadowing)
Hint3: if you really want to allocate and deallocate memory manually, use RAII (lookup RAII). Allocate in the constructor, and deallocate in the destructor

keen wind
#

Thank you very much!

iron valleyBOT
#

@keen wind Has your question been resolved? If so, run !solved :)

keen wind
#

!solved