Hey guys,
I'm getting a seg fault error when I run my program (the error comes with the assignNodes function), i'm realizing at this point im doing something wrong with making a new node to put the info in, but i'm still stuck on what to do. Do I need to write another line to create new nodes or make a different pointer? Here are my files. What i'm trying to do is read from a file and make an array of the info, then put that array into the nodes. Then repeat the process for every person (this is an ordered link list address book). Thanks!
Thanks!
Code:
/*
* addressbook.cpp
* Homework2
*
* Created by Alec on 10/2/10.
*
*/
using namespace std;
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include "SinglyLinkedList.h"
//String streams are similar to the <iostream> and <fstream> libraries,
// except that string streams allow you to perform I/O on strings instead of streams.
string upperCase(string lowerCase);
void assignNodes(string info[]);
int main()
{
int i;
ifstream fin;
string word;
string line;
string strInfo[10];
fin.open("address.dat");
while (getline(fin,line))
{
int i = 0;
cout << "Contact: " << line << endl;
stringstream stream(line);
while( getline(stream, word, ',') )
{
cout << upperCase(word) << endl;
strInfo[i] = upperCase(word);
i++;
}
assignNodes(strInfo);
}
fin.close();
return 0;
}
string upperCase(string lowerCase)
{
int i=0;
for( i = 0; i < lowerCase.length(); i++)
{
lowerCase[i] = char(toupper(lowerCase[i]));
}
return lowerCase;
}
void assignNodes(string info[])
{
contact *person;
person -> lastName = info[0];
person -> firstName = info[1];
person -> address1 = info[2];
person -> address2 = info[3];
person -> city = info[4];
person -> state = info[5];
person -> zipCode = info[6];
person -> phoneNum = info[7];
person -> dob = info[8];
person -> relation = info[9];
}
Code:
/*
* SinglyLinkedList.h
* Homework2
*
* Created by Alec on 10/2/10.
*
*/
# include <string>
using namespace std;
struct contact {
contact *head;
contact *first;
contact *last;
contact *next;
contact *back;
string lastName;
string firstName;
string address1;
string address2;
string city;
string state;
string zipCode;
string phoneNum;
string dob;
string relation;
};