Hi, I'm having a lot of trouble understanding the very basics of a linked list in c++. Can't find a lot of examples about saving file parameters in a linked list. After hours of tries and errors, I finally decided to submit a topic about my problem.
I'm trying to read a .don file who looks like this
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <set>
#include <locale>
usingnamespace std;
typedefstruct clients* PtrClients;
typedefstruct clients
{
int clientId;
float rebate;
PtrClients next;
PtrClients prec;
} clients;
//Find function
PtrClients findRebate(PtrClients list, int val, int nb)
{
list = NULL;
for (int i=1; i <= nb; i++)
{
cout <<"id="<< i << endl;
system("pause");
if (list->clientId == val) return list;
else
{
list = list->next;
}
}
return NULL;
}
int readCli(clients c[]){
int client;
float rebate;
int i =0;
// File
ifstream fileIn("clients.don", ios::in);
// Test
if (!fileIn) {
cerr << "Error opening the file" << endl;
system("pause");
exit(1);
}
while (fileIn>>c[i].clientId>>c[i].rebate){
i++;
}
fileIn.close();
return i;
}
int main()
{
clients cli[5];
int nbCli = readCli(cli);
PtrClients list = cli;
list=findRebate(list,2884,nbCli);
if (list == NULL)
cout << "Not found" << endl;
else
cout <<list<< endl;
system("pause");
}
Unfortunately my code doesn't work. What I need is to reach the rebate parameter (the float in the second column) by inserting the client parameter (first int) . I need to do that to node this file with another file (so , creating a double linked list) who is going to give me more information about every specific client. But, because I can't even catch a parameter, I'm pretty lost.
I hope to find a solution. Or at least a link when I can find everything needed.Thanks!
Hi, I'm having a lot of trouble understanding the very basics of a linked list in c++.
not shore if there is any reference to learn linked list but you may find it useful to have a look on my example of creating a linked list in this post (3.th reply) http://www.cplusplus.com/forum/beginner/55351/