Question: c++ How to read multi-word lines into an array from a text file using a while loop I have the following text file of names,

c++ How to read multi-word lines into an array from a text file using a while loop

I have the following text file of names, some include last names:

names.txt lusha soares rohan leo klein adrianna ada

And the following program which reads the names into an array:

#include #include #include #include //INCLUDE fstream

using namespace std;

int main() { const int NUM_EMPS = 5; string EMPS[NUM_EMPS]; int i = 0;

ifstream inputFile; inputFile.open("names.txt");

while (i < NUM_EMPS && inputFile >> EMPS[i]) { i++; } inputFile.close();

cout << "Employee names: " << endl; for (i=0;i

My issue is that it isn't reading a full name into one element in the array, only the first names, so when running the program my output looks like this:

Employee names: lusha soares rohan leo klein

How can I fix this so my output instead looks like:

lusha soares rohan leo klein adrianna ada

Any help would be appreciated!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!