Hi everyone,
I'm new to this site and figured I could get some assistance here. I'm a college student with slight programming experience. Anyways...
The program I need to create... needs to read a text file into an array of structures that are linked - hence a linked list. That text file the program will read is attached.
When everything from a1.txt is read, I need to sort the structures in ascending order by their SSN's. 'Obviously then outputting each structure after wards.
The posted code compiles with no errors, yet my program bugs out and crashes when the terminal window comes up. I'm hoping one of my for loops is messed up?
I'm quite new to linked lists and working with them. I use dev-C++ with Windows Vista by the way. Thank you for your time!
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
//const int maxname = 29;
//const int maxaddr = 69;
struct info
{
string name;
long SSN;
int age;
string address;
info *next;
};
int main()
{
const int max = 19;
info *resident[max];
string mySSN;
string myage;
string skip;
int i = 0;
ifstream inFile;
inFile.open("G:\\ECE370\\a1.txt");
while(!inFile.eof() && i <= max)
{
getline(inFile, resident[i]->name);
getline(inFile, mySSN);
resident[i]->SSN = atol(mySSN.c_str());
getline(inFile, myage);
resident[i]->age = atoi(myage.c_str());
getline(inFile, resident[i]->address);
getline(inFile, skip);
i++;
}
info *temp1;
info *temp2;
string tempname;
long tempSSN;
int tempage;
string tempaddress;
for(temp1 = resident[0]->next; temp1 != 0; temp1 = temp1->next)
{
for(temp2 = temp1->next; temp2 != 0; temp2 = temp2->next)
{
if(temp1->SSN > temp2->SSN)
{
tempname = temp1->name;
temp1->name = temp2->name;
temp2->name = tempname;
tempSSN = temp1->SSN;
temp1->SSN = temp2->SSN;
temp2->SSN = tempSSN;
tempage = temp1->age;
temp1->age = temp2->age;
temp2->age = tempage;
tempaddress = temp1->address;
temp1->address = temp2->address;
temp2->address = tempaddress;
}
}
}
for(int s = 0; s < i; s++)
{
cout << resident[s]->name << endl;
cout << resident[s]->SSN << endl;
cout << resident[s]->age << endl;
cout << resident[s]->address << endl << endl;
}
inFile.close();
system("PAUSE");
return EXIT_SUCCESS;
}