Question regarding reading a file into an array using a while loop

How do I use a while loop to read the file into the names array? I know I'm supposed to use getline() to read each name but I'm unsure how to approach the problem.

1
2
3
4
5
6
7
8
9
10
const int SIZE = 25;
string name[SIZE];
in.open("nameData.txt");
int count = 0;
while (count < SIZE && inputFile >> names[SIZE])
{
      getline(nameData, names);
      count++;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
   const int SIZE = 25;
   string name[SIZE];
   string line;
   int count = 0;
   
   ifstream in("nameData.txt");
   while ( count < SIZE && getline( in, line ) )
   {
      name[count] = line;
      count++;
   }
   in.close();


   // Check data
   cout << count << " names were read as follows:\n";
   for ( int i = 0; i < count; i++ ) cout << name[i] + "\n";
}
Topic archived. No new replies allowed.