I am new to C++ and am trying to make a function that takes an array of numbers and converts those numbers into the ASCII character, i.e. int to string. When I try to output the string however I get random characters. I have searched tutorials and a suggestion was that I should add a string terminator, which I did, but it doesn't seem to fix it and I can't find an answer that would solve this.
i.e. I want the below code to print "Hello".
#include <iostream>
#include <string>
char* intToString(int* array, int size)
{
char string[size + 1];
string[size] = '\0';
for (int i = 0; i <= size; i++)
string[i] = array[i];
return string;
}
int main()
{
int my_array[5] = {72, 101, 108, 108, 111};
int size = 5;
std::cout << intToString(my_array, size);
return 0;
}