Problem with reading from file

I am having an issue with the getline() function and the >> operator. I've not really found much concerning my specific issue and this is my first time posting so please be friendly.

I'm just bored and writing a program to help me keep track of a game using some linked lists and reading information from a file. My code that I'm using to get information from the file is:

if(head == NULL) //creates first node of list
{
head = (struct node*) malloc(sizeof(struct node));
current = head;
getline(infile, current->name);
cout << current->name << endl;
infile >> current->type;
cout << current->type << endl;
infile >> current->role;
cout << current->role << endl;
infile >> current->worshipers;
cout << current->worshipers << endl;
current->mastery = mastery(current->worshipers);
} //end if statement

infile is my ifstream name, current points to the current node in the list, my struct looks like this:

struct node
{
std::string name;
std::string type;
std::string role;
float worshipers;
float mastery;
struct node *next;
};
typedef struct node Node;
typedef Node *NodePtr;

This is only the code for the first node, but this is also where I am having an issue. For testing purposes, I have a simple text file that is as follows:

Dog
Bird
Monkey
4
3

The cout statements are there purely to tell me how far I've gotten before the program crashes. And every single time (regardless of my file or whether or not I use getline() vs >>), it will crash on the third file read. This means that I am unable to store anything into current->role, but both current->name and current->type work fine.

I'm certain my file opening is working as the first two lines are read in properly, and I've used my exact same file opener for other programs without an issue so as far as I can tell, the issue is somewhere within the block of code above, although I have no idea why it would simply crash when doing the same thing for a second time.

I may have to provide extra code for someone to really see the issue, so just let me know if I do. As I said, this if my first post and I'm hoping it's just a simple answer that I'm overlooking for whatever reason. Thanks for your help, everyone.
Topic archived. No new replies allowed.