Issues with reading stuff into a linked list

So I am having issue when it comes to searching a linked list. I am supposed to a read file in that has an album name, its price, and its year formatted exactly like below:

1
2
More than love 12.99 1989
Streets of London 5.99 1972


Now I am able to easily read this in using getline, but the issue is I also need to be able to search through each node. The user is supposed to be able to type in the album name, and it will return the price of the album and year. If I use getline it isn't going to be able to search through it with just the name.

This is what my linked list struct looks like and the rest of the class. I would assume it would be smart to also declare variable types for price and year but I am unsure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Entry {  
  std::string name;

  Entry *next;
};
class cdCollection{

public:
  Entry *head, *tail;
  cdCollection();
  ~cdCollection();
  void push_front(std::string temp);
  void push_back(std::string temp);
  void read_from_file(std::string fileName);
  void write_to_file(std::string fileName);
  int lookup(std::string s);
  void print();
  void delete_item(std::string s);
};


This is currently how I am reading from the file it works just fine but only causes issues later when I want to search.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void cdCollection::read_from_file(string fileName){
  ifstream inputFile;
  string temp; //this is for stroing what's in the file.

  inputFile.open(fileName);
  int size;
  
  if(inputFile.is_open()){
    while(inputFile){
      getline(inputFile,temp);//grabs the first line and throws it into the string variable temp
      push_front(temp);
    }
  }
  else{
    cout << "ERROR! Opening read file!" << endl;
  }
  inputFile.close();
}


This is the push forward function that the read function calls.

1
2
3
4
5
6
7
void cdCollection::push_front(string temp){
  Entry *insert = new Entry;
  insert -> next = NULL;
  insert -> name = temp;
  insert -> next = head;
  head = insert;
}


The only thing I could think to do would be to turn the string into a char array and if the next element is a int then the array ends there and then make a string. The problem is what if the album has a number in its name then that method doesn't work. I am absolutely stumped and would appreciate any help.
Last edited on
You could ask the user to surround that name in quotes and then use the quotes to tell where it begins and ends.
So the file is like
"More than love" 12.99 1989
"Streets of London" 5.99 1972
Last edited on
Topic archived. No new replies allowed.