-4

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!

Ken White
  • 117,855
  • 13
  • 197
  • 405
  • 1
    If this is your "first time using the site as a poster", then you should've taken the [tour], read the [help] and [ask] questions. But because you didn't, your shown code fails to meet all requirements for a [mre], and, as such, it is unlikely that anyone will help you, except to make random guesses as to what your problem might be. Most likely you're failing to correctly initialize whichever array is getting passed in, to be `strncat`-ed (and why are you using this old C library function, if your stated goal is to learn C++?), but since that code is not shown, that's only a guess. – Sam Varshavchik Nov 11 '19 at 01:52
  • Why are you using `strncat`? That appends one string to another. It looks like you want `strncpy`, which copies a string without requiring the destination memory to be initialized with a null-terminated string. – paddy Nov 11 '19 at 02:02
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – M.M Nov 11 '19 at 03:13

1 Answers1

0

Why are you using strncat? That appends one string to another. It looks like you want strncpy, which copies a string without requiring the destination memory to be initialized with a null-terminated string. – paddy

This was it. Swapping from using strncat to strncpy solved the issue.