Create a second constructor for Node
that accepts an istream
:
Node::Node(istream &is) { is >> Name >> Number; }
I'll leave the eof detection as an exercise for the reader.
Pregunta
I'm trying to read words and numbers from a file and insert them to Node struct. There is a file which includes StudentNames and StudentNumbers. I've created a struct like:
struct Node
{
string Name;
int Number;
Node *next;
Node::Node(){}
Node::Node(string ID, int No):Name(ID), Number(No){}
};
I'm trying to put Names and Numbers directly to Node while creating Node.
string filename, line,names;
int nos;
cout<<"Enter Filename: ";
cin >> filename;
ifstream input(filename);
if(input.fail())
{
cout<<"\n FAILED TO OPEN FILE";
}
else
{
getline(input, line);
stringstream word;
word>>names
word>>nos
Node *p;
p=new Node(names,nos);
cout<<p->Name<<p->Number;
}
My logic is that it reads words into names because they are string and it reads number parts into nos because nos is int it wont accept string only integers. But it wont names and nos are stay as "" idk how.
This is basically what I'm trying to do. I need to create a linked list with theese node containing names and numbers.
And another problem is I don't get how *next is pointing to the next node.
Sorry if its confusing but I'm confused too :))
Input File Example
L0RDQB 12345
LordQb 23456
...
No hay solución correcta
Otros consejos
Create a second constructor for Node
that accepts an istream
:
Node::Node(istream &is) { is >> Name >> Number; }
I'll leave the eof detection as an exercise for the reader.