Solution:
Hello Gavin,
This is not a noob question at all but an interesting one. Yeah, you take input from a text file and save it into an array.
I am trying to shed some light on this but let me show you the sample code first:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string array[2];
short loop=0;
string line;
ifstream myfile ("Test.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
array[loop] = line;
cout << array[loop] << endl;
loop++;
}
myfile.close();
}
else cout << "can't open the text file";
system("PAUSE");
return 0;
}
Well, here it is. Now, let's try to decode our program. The three header files are used for the following:
input-output stream, input, and output operations and use to create files, and saving the line into the array.
Dive into the main function, we created a string array to hold the names inside the array and the string line will store the data from the text file up to two values using the for-loop.
I hope, this little explanation will help you to get the program well.