Read files into linked list

Hello, I'm having trouble reading a cdv file into a linked list, I started writing the code but I'm not sure if on the right track and if I am i don't know how to continue,
the csv file represents accounts, the format is

date, first name, last name, account number, type, initial balance

and it can be of any length


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//LIBRARY.H FILE
using namespace std;	// for cout and cin

enum AccountType { CHECKING, SAVINGS };

struct Date 
{
	int month, day, year;
	int hour, minute, second;
};

struct Account 
{
       string date;
       string firstName;
       string lastName;
       long number;
       string type;
       double amount;
};

struct AccountNode 
{
       Account* account;
       AccountNode* next;
};

struct Node 
{
	Account* account;
	Node* next;
};

Date getCurrentDate();
void displayAccounts ( AccountNode* listOfAccounts );
string parseCsvRow ( string row, int commaOffset );
Account* createAccount(string date, string fn, string ln, long num, string type, float amount);
void push ( Node** list, Account* account );
AccountNode* loadAccountsFromFile(string fileName);
[code]


[code]
\\LIBRARY.CPP FILE
string parseCsvRow ( string row, int commaOffset )
{
	int commaIndex = row.find(",",0);
	if ( commaIndex == -1 )
		return "";
    
	commaOffset++;
	row += ",";
    
	commaIndex = 0;
	int lastCommaIndex = 0;
	string data;
	for(int i = 0; i < commaOffset; i++ ) {
		lastCommaIndex = commaIndex;
		if(lastCommaIndex > 0) {
			lastCommaIndex++;
		}
		commaIndex = row.find(",",lastCommaIndex);
		if(commaIndex == -1)
			return "";
		data = row.substr(lastCommaIndex, commaIndex - lastCommaIndex);
	}
	return data;
}

Account* createAccount(string date, string fn, string ln, long num, string type, float amount)
{
             Account* newAccount = new Account;
             newAccount->date = date;
             newAccount->firstName = fn;
             newAccount->lastName = ln;
             newAccount->number = num;
             newAccount->type = type;
             newAccount->amount = amount;
             return newAccount;           
}

AccountNode* createList ( Account* account ) 
{
	AccountNode* newNode = new AccountNode;
	newNode->next = NULL;
	newNode->account = account;
	return newNode;
}

void push ( AccountNode** list, Account* account ) 
{
	AccountNode* newNode = new AccountNode;
	newNode->next = *list;
	newNode->account = account;
	*list = newNode;
}

AccountNode* loadAccountsFromFile(string fileName)
{
        Account* accountNode = new Account;        
        AccountNode* h = createList ( accountNode );       
        push(&h, accountNode);                     
}

void displayDate ( Date date ) 
{

	cout << date.month << "/" << date.day << "/" << date.year << " ";
	cout << date.hour << ":" << date.minute << ":" << date.second;
}

void displayAccount ( Account n ) 
{
	cout << n.date << ", " << n.firstName << ", " << n.lastName << ", " << n.number << ", " << n.type << ", " << n.amount << endl;
}

void displayList ( AccountNode* list ) 
{
	AccountNode* current = list;
	do {
		Account* n = current->account;
		displayAccount ( *n );
		current = current->next;
	} while ( current != NULL );
}       

[code]

[code]
/MAIN FILE
int main()
{
    string date[100];
    string firstName[100];
    string lastName[100];
    long num[100];
    string type[100];
    double balance[100];
    string fileName;
    
    cout << "Please enter the file name: ";
    cin >> fileName;
    fileName+= ".csv";

    ifstream accountFile;
    accountFile.open (fileName.c_str());
    string row;
    
    for( int r = 0; r < 10; r++ )
    {
         getline (accountFile,row);
         date [r] = parseCsvRow ( row, 0 );
         firstName[r] = parseCsvRow ( row, 1 );
         lastName[r] = parseCsvRow ( row, 2);
         num[r] = atol(parseCsvRow ( row, 3 ).c_str());
         type[r] = parseCsvRow ( row, 4 );
         balance[r] = atof ( parseCsvRow ( row, 5 ).c_str());                
    }
    
    
    Account *n = createAccount (date[0], firstName[0], lastName[0], num[0], type[0], balance[0]);
    AccountNode* list = createList (n);
    displayList ( list );
    getch();
    

    accountFile.close();
}



Id really appreciate your help, please and thank you!!
Topic archived. No new replies allowed.