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;
}
Attachments
April,Joe
312033387
21
126 Freelane, Yourtown, State 88990

Matthews,Jocob
459237148
19
3930 4th Blvd, Yourcity, NJ 88710

Garfield,Kitty
891772436
24
36 Jon Havey Court, Middle, MO 66222

Lake,Bill
223390100
32
21 Ritts, Middletown, MI 48788

Johnson,Jim
567218923
22
100 Lakeview Road, Youngstown, MI 48932

Zumistan,Kim
675098667
25
76 Pond Street, New Brunswick, NJ 31900
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++;
    }

I think you need to set resident[i-1]->next in here somewhere.

I think you need to set resident[i-1]->next in here somewhere.

Thanks for helping!

I tried doing what you are suggesting, is this what you mean? (I made the additions to the bottom of the loop, and outside it by one line. 2 additions total)

while(!inFile.eof())
    {
    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++;
    resident[i] = resident[i-1]->next;
    }
    resident[i]->next = NULL;

I try compiling and it passes again without errors, yet it is still crashing. Is there anything wrong with what I'm doing?

T

while(!inFile.eof())
    {
    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++;
    resident[i] = resident[i-1]->next;
    }
    resident[i]->next = NULL;

I try compiling and it passes again without errors, yet it is still crashing. Is there anything wrong with what I'm doing?

what are you tryint to do here

i++;
 resident[i] = resident[i-1]->next;

for i = 0 you will increment i first so i = 1 then you say
resident[1] = resident[0]->next; resident[0]->next was not set to any value and NULL so resident[1]->next is also NULL and so on and so forth. In a normal forward link list the first element has the link to the second element and so on. so while inserting you need to handle the case of the first node separately. The last node's 'next' pointer should be NULL only.

avoid using eof(), Read This.

And why do you need an array of info*'s ?? This seems like a hybrid of arrays and linked list !!!

Just as a comment to the whole process...

You declared info *resident[max]; (thats an array of pointers to info) but I don't see where you ever allocated the info instances to put in the array.

(I suspect this is the root cause of your 'bugging out')

Also, Agni is right, for a linked list implementation, you commonly only keep the 'head' node pointer, you don't keep an array of pointers.

In your code, you seem to treat your collection as an array in the input function, you use linked list iteration, but swap the data instead of swapping nodes; and treat the collection as an array again for output.

Thanks for the replies.

I see I need to rework this code a lot. Arrays are in my code because I was just gonna use an array of structures, but realized link lists were a better option for what I ultimately need to do, and switched to that method. For some reason I decided to leave the array method in. Don't ask. 0_o

This is only a fraction of an entire program I need to create. (i.e. ability to search/add/delete nodes)

I'll be back with updated code hopefully soon.

This article has been dead for over six months. Start a new discussion instead.