Input multiple data types from a file into a linked list
-
i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)
void double_llist::loadfromfile(){
string mname,mgenre,mlanguage; int mrelease\_year,mamount, mrating,mruntime; ifstream fromfile; fromfile.open("database.txt"); while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime) { /\* do something with name, var1 etc. \*/ cout << mname<
the code for doublylinkedlist
void double_llist::create_list(string mname,
string mgenre,
string mlanguage,
int mrelease_year,
int mamount,
int mrating,int mruntime)
{struct node \*s, \*temp; temp = new(struct node); temp->info=id++; temp->name = mname; temp->genre=mgenre; temp->language=mlanguage; temp->release\_year=mrelease\_year; temp->amount=mamount; temp->rating=mrating; temp->runtime=mruntime; temp->next = NULL; if (start == NULL) { temp->prev = NULL; start = temp; } else { s = start; while (s->next != NULL) s = s->next; s->next = temp; temp->prev = s; }
}
my class and node struct for linkedlist
#include
#include
#include
#include
#include
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
string name;
string genre;
string language;
int release_year;
int amount;
int rating;
int runtime;
struct node *next;
struct node *prev;
static int counter;
}*start;
int id=1;
/*
Class Declaration
*/
class double_llist
{
public:void create\_list(string mname, string mgenre, string mlanguage, int mrelease\_y
-
i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)
void double_llist::loadfromfile(){
string mname,mgenre,mlanguage; int mrelease\_year,mamount, mrating,mruntime; ifstream fromfile; fromfile.open("database.txt"); while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime) { /\* do something with name, var1 etc. \*/ cout << mname<
the code for doublylinkedlist
void double_llist::create_list(string mname,
string mgenre,
string mlanguage,
int mrelease_year,
int mamount,
int mrating,int mruntime)
{struct node \*s, \*temp; temp = new(struct node); temp->info=id++; temp->name = mname; temp->genre=mgenre; temp->language=mlanguage; temp->release\_year=mrelease\_year; temp->amount=mamount; temp->rating=mrating; temp->runtime=mruntime; temp->next = NULL; if (start == NULL) { temp->prev = NULL; start = temp; } else { s = start; while (s->next != NULL) s = s->next; s->next = temp; temp->prev = s; }
}
my class and node struct for linkedlist
#include
#include
#include
#include
#include
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
string name;
string genre;
string language;
int release_year;
int amount;
int rating;
int runtime;
struct node *next;
struct node *prev;
static int counter;
}*start;
int id=1;
/*
Class Declaration
*/
class double_llist
{
public:void create\_list(string mname, string mgenre, string mlanguage, int mrelease\_y
A couple of minutes with the debugger would have shown you that you cannot enter text like that and expect the system to realise that the film title comprises more than one word. Stream input breaks fields up at white space so in your first case you will have something like:
mname = SHAWSHANK
mgenre = REDEMPTION
mlanguage = 120
mrelease_year = 5
mamount = MYSTERY - which is not an integer
mrating = 2009
mruntime = ENGLISH - also not an integerthe final 3 will now go into the title next time round the loop.
-
A couple of minutes with the debugger would have shown you that you cannot enter text like that and expect the system to realise that the film title comprises more than one word. Stream input breaks fields up at white space so in your first case you will have something like:
mname = SHAWSHANK
mgenre = REDEMPTION
mlanguage = 120
mrelease_year = 5
mamount = MYSTERY - which is not an integer
mrating = 2009
mruntime = ENGLISH - also not an integerthe final 3 will now go into the title next time round the loop.
yes i know that's why i am asking a solution for that.
-
yes i know that's why i am asking a solution for that.
You need to split the lines manually so that you can separate the title from the other fields. You could just read the entire line, tokenise it into an array, and then work backwards field by field. Whatever is left at the beginning of the array can be recombined into a single string which is the title.
-
i am working on a movie database and i have output from my file that i want to load into my linked list,no problem opening file but when i run this function it enters junk values in the list and only works if i enter all the strings without spaces or seperated by special characters. i am reading this from the txt file SHAWSHANK REDEMPTION 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART1 120 5 MYSTERY 2009 ENGLISH 3 THE MATRIX PART2 120 5 MYSTERY 2009 ENGLISH 3 this is the code for load text from file into a linkedlist(doubly)
void double_llist::loadfromfile(){
string mname,mgenre,mlanguage; int mrelease\_year,mamount, mrating,mruntime; ifstream fromfile; fromfile.open("database.txt"); while (fromfile >> mname >> mgenre >> mlanguage >> mrelease\_year>>mamount>>mrating>>mruntime) { /\* do something with name, var1 etc. \*/ cout << mname<
the code for doublylinkedlist
void double_llist::create_list(string mname,
string mgenre,
string mlanguage,
int mrelease_year,
int mamount,
int mrating,int mruntime)
{struct node \*s, \*temp; temp = new(struct node); temp->info=id++; temp->name = mname; temp->genre=mgenre; temp->language=mlanguage; temp->release\_year=mrelease\_year; temp->amount=mamount; temp->rating=mrating; temp->runtime=mruntime; temp->next = NULL; if (start == NULL) { temp->prev = NULL; start = temp; } else { s = start; while (s->next != NULL) s = s->next; s->next = temp; temp->prev = s; }
}
my class and node struct for linkedlist
#include
#include
#include
#include
#include
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
string name;
string genre;
string language;
int release_year;
int amount;
int rating;
int runtime;
struct node *next;
struct node *prev;
static int counter;
}*start;
int id=1;
/*
Class Declaration
*/
class double_llist
{
public:void create\_list(string mname, string mgenre, string mlanguage, int mrelease\_y
Farhan_Karim wrote:
works if...all the strings...seperated by special characters.
Is that not a possibility?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
yes i know that's why i am asking a solution for that.
The easy solution is to override the read and write methods in your own stream extension. It is the "standard", "usual" and "prefered" way to fix behaviour of classes or objects with methods and functions that aren't exactly as you desire. Can I go so far as to suggest MovieNameStream as a possible title for the extension.
In vino veritas