-1

I am trying to read a file in from a text document into 2 arrays. I've ruled out the problem being in my other functions or my mains, and that leave about 5 lines of code....

It will cycle through my document to the end, but it only inputs the txt once through the loop. Any thoughts would be great!

void load_donations(string donor[], string donation[])
{

    string text;

    cout << "What *.txt file would you like to load? ";
    cin >> text;
    text += ".txt";
    cout << text << endl;
    ifstream infile;
    infile.open (text.c_str());
    int i = 0;    //moves to next slot in array

    while (!infile.eof())
    {

        getline(infile, donor[i]);
        getline(infile, donation[i]);
        i++;

    }

    infile.close();
}
frslm
  • 2,977
  • 3
  • 10
  • 25
Hooch
  • 33
  • 7
  • wont this work donation[i] = donor[i]? – PrimeOfKnights Oct 06 '17 at 21:56
  • I'm not sure what you are saying I should do... But what I want is my .txt file to read like this. Jimmy fine Shoes Henry Cash Mr Biggles Food Steve time Then every even line going into donor[i] and odd line going into array[i]. My issue is it only seems to be doing one loop. If I cout a loop counter it will show it goes through all the lines, its just not saving them into the array. – Hooch Oct 06 '17 at 21:59
  • Unrelated[Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Oct 06 '17 at 22:03
  • Hooch, as you can probably tell, comments suck smurf at formatting. Add your file sample to the question with an edit so you can maintain the file's formatting. – user4581301 Oct 06 '17 at 22:04
  • A handy trick is to not use multiple arrays. Make a structure out of the data that foes in the arrays and make a single array of the structure. It's much easier to keep related data lined up if it is all in the same logical block of memory. – user4581301 Oct 06 '17 at 22:10
  • Unfortanately I'm not allowed to use a dynamic array for this... Or structures. – Hooch Oct 06 '17 at 22:17
  • 1
    You haven't provided enough information. You need to provide an [mcve]. You also need to include, in your post, a representative input file that gives the problem, as well as a description of what output you expect and how the input differs. Yeah, okay, you have identified the function where the problem seems to be. But you've also left out information that is relevant to helping OTHER people understand the problem. – Peter Oct 06 '17 at 22:37
  • God I'm stupid.... As I went to create a minimal I realized that I wasn't passing my count back to another function... Thank you all for the quick responses and help though! – Hooch Oct 06 '17 at 22:51

2 Answers2

1

The problem was that I wasn't passing my count from this function to the function that printed out my array.. I only noticed it when I went to create a minimal like Peter suggested. Feel free to kick me on my way out.

Hooch
  • 33
  • 7
  • This is why the MCVE is part of the toolbox of every successful programmer. Or every one I know, anyway. Strongly recommend fixing the `while (!infile.eof())` bug while you're at it. Use `while (getline(infile, donor[i]) && getline(infile, donation[i])) { i++; }` – user4581301 Oct 06 '17 at 22:57
-1

For this, you need to declare dynamic array of chars, and Then read from file character by character and save that characters in dynamic array inside the loop.

Does this make sense to you ?

U.Malik
  • 81
  • 11