Mary Wilson 1234 2.99 Allen Harris 1235 3.65 Mike Davis 1236 2.65 Mary Ann 1237 4.02 Allen Brown 1238 3.65 Joe Smith 1239 2.65
This is what I am trying to read. A string firstName, string lastName, string ID, double GPA.
Below is what I have come up with.
#include <fstream>
#include <iostream>
#include <list>
using namespace std;
struct studentRec
{
string lastName;
string fistName;
string ID;
double GPA;
studentRec *next; //pointer to next item.
};
int main()
{
studentRec *studentProfiles = NULL; //head node set to null currently.
ifstream datafile("StudentRecords.txt");
if(!datafile)
{
cout << "Error in opening the file of profiles.";
exit(1);
}
//Read the file into a linked list.
studentRec profile; //Make an instance of studentRec, call it profile.
cout << "The contents of the file are: " << endl;
while(!datafile.eof()) //While not end of file keep reading next item.
{ //Below, read in instance of lastName, firstName, ID, GPA.
datafile >> profile.lastName >> profile.firstName >> profile.ID >> profile.GPA;
studentProfiles.push_back(profile); //Take that instance of profile, add to node using function push_back.
}
datafile.close(); //Close data file, continue program.
}
The comments in the code represent what I am thinking. So please tell me what I am thinking wrong.
Basically as I see it, a linked list contains a head node, the place to start, and each node points to the next node. So I can read in item by item into an instance of my structure and then push back the amount of elements in the list. It isn't working how I wanted though.
cpp(30) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Is the fist error that comes up. However this worked fine in an older program where I was using vectors and the same method to read data into the vector. Any insight into this problem?
Any help at all would be great. Even a small tutorial on making linked list of structures like I am. Couldn't find any on google. I am still learning everything about linked lists so please be patient with me.

New Topic/Question
Reply



MultiQuote




|