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!