General c++ linked list question

maclem8223

[H]ard|Gawd
Joined
Oct 28, 2013
Messages
1,849
Hello all,

I'm not sure on the code formatting for [H] so I apologize in advance (trying to use
Code:
 and  [\code] doesn't seem to be working)...I'm in a c++ class where we have a inputfile with movie title, actor, etc.  We are to setup a struct and utilize nodes and pointers to establish a linked list and then output to a file the list formatted.  The issue I'm having and where I'm not sure I'm fully understanding the concept is in trying to access the list from main.
[B]Here is my header with the struct.
[/B][code]
#ifndef HEADER_H_
#define HEADER_H_
#include <iostream>
#include <iomanip>
#include <string>
#include <ostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <limits>

using namespace std;

struct MovieNode
{
	string    title;
	string    actor;
	string    supActor;
	string    genre;
	string    altGenre;
	int         year;
	int    	     rating;
	string    synop;
	MovieNode *next;
};

MovieNode *ReadIntoList(const string INPUT_FILE);


#endif /* HEADER_H_ */

The function to create the list:

Code:
#include "header.h"

MovieNode *ReadIntoList(const string INPUT_FILE)
{
	ifstream inFile;
	MovieNode *head;
	head     = NULL;
	MovieNode *perPtr;
	perPtr = new MovieNode;

	inFile.open(INPUT_FILE.c_str());

	while(inFile && perPtr != NULL)
	{
		getline(inFile, perPtr->title);
		getline(inFile, perPtr->actor);
		getline(inFile, perPtr->supActor);
		perPtr -> next = head;
		head = perPtr;
		perPtr = new MovieNode;
	}

	inFile.close();
	delete perPtr;
	perPtr = NULL;

	return head;
}

and then main.
Code:
#include "header.h"

int main()
{
	//PrintHeaderStream - Outputs the print header to console.
	cout << PrintHeaderStream("Maclem", "Intro to Linked Lists",
							  'A', 4);
	string inputFile;
	MovieNode *head;

	cout << left;
	cout << "Please enter the name of the input file you would like to "
			"use: ";

	getline(cin, inputFile);

	head = ReadIntoList(inputFile);

//	OutputToFile(head);


	return 0;
}

When I try to cout or cerr head for example:
cout << head->title; I'm not getting anything so I think I'm confused on how to return the head and how to access the list... Any help would be appreciated and I'm sorry about the formatting.
 
Last edited:
Anybody? Basically if I try and get the value of head in the while loop it outputs correctly but trying to access head from anywhere else I'm receiving no output. If someone could explain to me about the
Code:
 for formatting I'd be happy to fix the mess I posted...
 
You have to use [code][/code] not [code][\code]

From what I can tell it's because your while loop tries to read in one more "node" even though it already reached the end of the file. So you should be testing for that with inFile.eof().

So in the end you end up with 1 extra node at the front of your list with empty strings in them.

Code:
while(inFile && !inFile.eof() && perPtr != NULL)

Next time try printing out the entire list and it becomes clear there was an extra empty node

Code:
void PrintList( MovieNode* head ) {
  while( head ) {
    std::cout << "Title: " << head->title << std::endl;
    std::cout << "Actor: " << head->actor << std::endl;
    std::cout << "Supporting Actor: " << head->supActor << std::endl;
    head = head->next;
  }
}
 
Thank You so much for your help. And sorry for the dumb mistake. Code is fixed...I'm on to outputting
to a file, learning how to traverse the list and figuring out word wrap. I might be back later lol...thanks again.
Code:
void PrintList( MovieNode* head ) {
  while( head ) {
    std::cout << "Title: " << head->title << std::endl;
    std::cout << "Actor: " << head->actor << std::endl;
    std::cout << "Supporting Actor: " << head->supActor << std::endl;
    head = head->next;
  }
}
the std::cout syntax we have not learned in class so I wouldn't be able to use it.
 
Last edited:
the statement "using namespace std;" you have in the first header file eliminates the need to preface cout with std::, std is the namespace.
 
Back
Top