0
int main() {
  char arr[10] = {};
  string str;
  arr[0] = 'h';
  arr[1] = 'e';
  arr[2] = 'y';

  for (int i = 0; i < 10; i++) {
    str[i] = arr[i];
  }

  cout << str;

}

Why this code is not printing hey? I didn't forget to include the libraries I just put the important part of the code. when I try to cout str[1] as an example it cout 'e' but when I try to cout the string as it is it doesn't print anything.

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
John
  • 1
  • 1
  • `str[i]=toto[i];` invokes *undefined behavior*. The proper way to add characters to a `std::string` is via `str.push_back(toto[i]);` – UnholySheep May 01 '22 at 22:00
  • `str[i] = arr[i];` might work if the string had enough space allocated. But `string str;` doesn't allocate any space at all. You could `string str(10);` and then it would have enough space allocated. It's easier to just `string str(arr);` after `arr` has been filled in. – Jerry Jeremiah May 01 '22 at 22:16
  • Also consider that you have no null terminator at the end of your char array string. `arr[3]='\0';` would fix that. You also don't need an explicit loop, `str = arr;` will do the work. – Paul Rooney May 01 '22 at 22:18
  • 1
    @PaulRooney As the array is initialized with {}, it already contains the null terminator since only the first 3 characters are modified to something else than the null character. – Phil1970 May 01 '22 at 23:05
  • Ok fine. Maybe I’m a bit old school and prefer to be explicit. – Paul Rooney May 01 '22 at 23:08
  • I'm not sure why any of us are populating `arr` one character at a time. – Paul Sanders May 01 '22 at 23:14
  • Does this answer your question? [I can't double characters inside string with function](https://stackoverflow.com/questions/63951606/i-cant-double-characters-inside-string-with-function) – JaMiT May 02 '22 at 02:03

1 Answers1

0

I apologize in advance if my explanation is a bit vague but you forgot to include the <iostream> and <string> libraries in your header. Also, since you are using the string library, you must include the using namespace std; within the scope of your string str;. There is a bit more optimization that you can do to your code. For example, in this case, it is optimal to iterate up to the size of your array instead of iterating through to 10 as most of your array is empty. Also, because you are adding char values to a string object, you can just concatenate the empty string with the value at arr[i].

#include <iostream>
#include <string>
int main () 
{
    using namespace std; //must include this as string object requires it
    char arr[10];
    string str;
    arr[0]='h';
    arr[1]='e';
    arr[2]='y';

    for (int i = 0; i < sizeof(arr); i++)
    {
        str += arr[i]; //adds arr[i] to the end of str
    }
    cout << str; //prints string
}

I hope this helps and was clear enough!

  • *'you forgot to include the and libraries in your header."* -- no, the question explicitly states *"I didn't forget to include the libraries"*. So the OP did not forget the headers; the OP intentionally omitted them, with the result of making it tougher to reproduce the error. (A bad decision, I think.) – JaMiT May 02 '22 at 02:09
  • *"you must include the `using namespace std;` within the scope of your `string str;`."* -- no, that is not necessary (i.e. not "must"; it's an option, but not the only one). Another option is to use a line like `using std::string;`, which limits the potential for [problems associated with `using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). A third option is write out `std::string` instead of merely `string`. – JaMiT May 02 '22 at 02:13
  • *"For example, in this case, it is optimal to iterate up to the size of your array instead of iterating through to 10 as most of your array is empty."* -- the size of the array **is** 10. This is a place that could be optimized, but your execution is lacking. – JaMiT May 02 '22 at 02:19
  • *"Also, because you are adding char values to a string object, you can just concatenate the empty string with the value at `arr[i]`."* -- good start. Even slicker: you could make sure there is a null terminator (e.g. `arr[3] = '\0';`) then concatenate the strings in one line (`str += arr;`). – JaMiT May 02 '22 at 02:22