Reading text file into linked list

Write your question here.
I am working on a program that requires me to read a text file and insert the data into a linked list. I am having difficulty reading the data into the linked list the code compiles, but nothing appears in the output. Any help or advice would be greatly appreciated.

The text files format is as followed: string int int int int int int int int int int int int

A 83 140 228 286 426 612 486 577 836 0 0 Aaliyah 0 0 0 0 0 0 0 0 0 380 215

Aaron 193 208 218 274 279 232 132 36 32 31 41

Abbey 0 0 0 0 0 0 0 0 537 451 428
use linked list for the internal data structure.

linked_list<NameSurferEntry> database;
This is the function I use to parse each line in the file.

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
  NameSurferEntry::NameSurferEntry(string line){
    vector<string> filelines;

    stringstream inputstringstream;

    year.push_back(1900);
    year.push_back(1910);
    year.push_back(1920);
    year.push_back(1930);
    year.push_back(1940);
    year.push_back(1950);
    year.push_back(1960);
    year.push_back(1970);
    year.push_back(1980);
    year.push_back(1990);
    year.push_back(2000);
    year.push_back(2010);

    while(getline(inputstringstream, line,' ')){
        filelines.push_back(line);
    }
    for(unsigned int i=1; i < filelines.size(); i++){
        cout << year.at(i-1) << " " << filelines.at(0 + 1) << "\n";
    }
        cout << " " << endl;

}

string NameSurferEntry::getName(){
    return name;
}

int NameSurferEntry::getRank(int decade){
    return year.at(decade);
}
and this is the function I use to read the text file.

NameSurferDataBase::NameSurferDataBase(string filename){
     ifstream input;
    input.open(filename);
    if(!input.is_open()){
        cout << "Not Open";

    }else{
        string temp;
        while(!input.eof()){
            getline(input,temp);
            if(!input.eof()){
                NameSurferEntry entry(temp);
                database.InsertInOrder(entry);
            }
        }
    }
Hi Pumps,
You were very close ^_^

You just inverted the two variables in the getline :
while(getline(inputstringstream, line,' ')){ ...

I think you wanted to something like :
1
2
3
4
5
std::string part;
while(getline(line, part, ' '))
{
    filelines.push_back(part);
}

Few comments :
* the variable "inputstringstream" is not usefull, get rid of it.
* "year" : I don't know why you make such a computation with a vector etc. for getRank(). Wouldn't it be enough to make a function like
1
2
3
4
int getRank(int decade)
{
	return 1900 + 10 * decade;
}

If you really want to keep that same procedural logic, at least, put that in a function and don't mix it with the NameSurferEntry class.
And then, don't create a "list" of years each time you need one... just compute that only once in a static member variable for example. The best would be a function with a static variable. For example :
1
2
3
4
5
int getRank(int decade)
{
	static std::vector<int> year = { 1900, 1910, 1920 };
	return year.at(decade);
}
But I don't see the point at all of all that complicated thing ^_^

That's really all, I think ^_^
Have fun
Last edited on
Topic archived. No new replies allowed.