Reading a text file into an array

lastname Taylor marie denise
james wilis surname thomas
Stone Rock lastname Brown
surname lea high lee
stephens lastname reynolds
lastname mack russell
surname Lewis Michelle Tee
john mark surname marshall
lastname Moto ahsey
o e lastname vey
Twosee or surname knocksee

lastnames.txt





Hello all. I am working on a project where I am to read in the list of names in a text file and put them into an array. I am also supposed to sort the list of names by last name first. The list of names prints out in the console, but then something causes the program to terminate unexpectedly. The code is not complete by any means, I just wanted to see why it was terminating the way it is just from reading in the text file and putting it into the array. Any suggestions?

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;

string inputname(string);
string namecopy(string);
string printarray(string);


ifstream infile;
ofstream outfile;

string inputname(string namearray[])
{
while(!infile.eof())
{
for (int i=0;!infile.eof(); i++)
{
getline(infile, namearray[i]);
cout << namearray[i]<< endl;
}
}
}

string namecopy(string namearray[], string copyarray[])
{

for (int i=0;i<15;i++)
{
copyarray[i] = namearray[i];
}
}

string printarray(string namearray[])
{
for(int i=0;i<15;i++)
{
if (namearray[i] != " ")
{
outfile << namearray[i] << endl;
}
}
}

int main()
{

string names[15];
string names2[15];

outfile.open ("namesarrangeout.out");
if (!outfile)
{
outfile<< "Output file did not open. \n";
return 1;
}
else
{
outfile<< "Output file did open. \n";
}

infile.open("lastnames.txt");
if (!infile)
{
outfile<< "Input file did not open. \n";
return 1;
}

inputname(names);


return 0;
}
Hi, and welcome:) What is the error-message you are getting?

Also, it's best to use the code-tags when posting code like this:)

Fafner
Sorry bout that. The error message I am getting is this:main.exe has stopped working.

When I click "click here to close the program" it goes back to the console where the array appears.

The array appears in the console but with Process returned -1073741819.

I am using Code Blocks 10.05.
Topic archived. No new replies allowed.