I am in a c++ intro programming course and I'm having problems with the titled question. I am reading a text file of Haikus (all student submitted, I know half of them aren't even proper haikus) into a 2D array using c-type strings (a requirement of the assignment, so I would prefer answers focus on using c-type strings in the replies).
The issue arises when I am outputting what is stored in the array to confirm everything is copied properly. Once it starts printing anything at array[I] where I>40 it starts to print gibberish.
I've narrowed it down to it starting to print gibberish at row 40 an by line 42 it is complete gibberish. The file is opening correctly and I am checking for file corruption for every instance of the loop.
//function for extracting text file into array
void getHaikus (ifstream& in, char haikuArray[][HAIKU_ARRAY_COL])
{
int i, j;
char str[50]; //ctype string variable
for(i=0;i<HAIKU_ARRAY_ROW && !in.eof();i++)
{
in.getline(str, 50); //read line into temp string
strncat(haikuArray[i], str, 50); //concatenate temp string into array
if(!in.good()) //check for file corruption during loop
{
cout << "file corrupt mid extraction\n";
cout << "i value is: " << i << endl;
exit(1);
}
}
return;
}
Expected output:
To sit on blue *
Surronded, * birds rest,
* one last time.
.
.
.
Autumn is a *
And a very * one
* all the time
A candlelit *,
Cause * pumpkins to glow,
* October.
My * was with Nic
And they both like the *
* is awesome
Actual output:
To sit on blue *
Surronded, * birds rest,
* one last time.
.
.
.
Autumn is a *
And a very * one
* all the time
A candlelit *,
¼Cause * pumpkins to glow,
* October.
¾My nd they both like the *
o_Ò¾* is awesome
array[40] begins with the line "Cause * pumpkins…"
Deepest apologies to any poor formatting, this is my first time using the site as a poster. Thanks for any help you can provide!