Use [ code ] [/ code ] (without spaces) tags to post your code.
Is this what you want....
You can simultaneously read line and put it into array[] and line, so you dont need two loops.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string array[5]; // creates array to hold names
short loop=0; //short for loop for input
string line; //this will contain the data read from the file
ifstream myfile ("example.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
array[loop] = line;
cout << array[loop] << endl; //and output it
loop++;
}
myfile.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output
system("PAUSE");
return 0;
}
Last edited by vishesh; Mar 6th, 2007 at 6:34 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
Offline 1,362 posts
since Oct 2006