[Solved] C++ Array out of strings


Since the length of the array is not known at compile time, you can not use an automatic array. The array has to be allocated dynamically. The simplest solution to copy a string into an array would be to use std::vector.

However, I suspect that your desire to create an array from a string is ill-founded. You probably don’t need to do it. You could rewrite your loop like this:

cin >> stringToGuess;
for (char c : stringToGuess) { 
    cout << c << " ";
}

char arr_To_Guess[arrLength]

The problem I mentioned earlier, arrLength is not known at compile time, so this declaration is ill-formed.

= {atoi(stringToGuess.c_str())};

This is very strange. You attempt to initialize the first character of the new array to have the numeric value of whatever the string represents. For example:

string: "123"
becomes: [123, 0, 0]

I suspect that this is not what you intended.

0

solved C++ Array out of strings