?
Solved

ofstream variable issue: doesn't print out everything it reads

Posted on 2012-08-28
3
Medium Priority
?
467 Views
Last Modified: 2012-08-29
Hi, I am having trouble with my ofstream variable at the last part of my code. How can I avoid loosing the information from the ofstream?
The issue is generated by the very last part of the program.

here is the output that I get:
Action Movies

Comedy Movies

Drama Movies

Horror Movies
There are no movies for this genre

Mystery Movies
There are no movies for this genre

Romantic Movies

Adventure Movies

Western Movies

ife 1946 D 130 30 8.6 1946 103101
 The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648
 Lawrence of Arabia 1962 V 216 48 8.5 1962 80746
 For a Few Dollars More 1965 W 132 123 8.2 1965 45515

Open in new window



And here is what I should be getting:
Action Movies
Braveheart 1995 A 177 88 8.3 1995 245089
Batman begins 2005 A 140 110 8.3 2005 275523

Comedy Movies
Amelie 2001 C 122 45 8.5 2001 185124
Singin' in the Rain 1952 C 103 77 8.3 1952 56368

Drama Movies

it's a Wonderful Life 1946 D 130 30 8.6 1946 103101

Horror Movies
There are no movies for this genre

Mystery Movies
There are no movies for this genre

Romantic Movies
The Best Years of Our Lives 1946 R 172 181 8.1 1946 17648

Adventure Movies
Lawrence of Arabia 1962 V 216 48 8.5 1962 80746

Western Movies
For a Few Dollars More 1965 W 132 123 8.2 1965 45515



here is the printMovieInfo() from the class
 
void movieType::printMovieInfo(char* outFileName)
{
	std::ofstream outFile;
	if(!outFile.is_open()) 
		outFile.open(outFileName, std::ios::app);
	outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank;
	outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl;
	
}

Open in new window


and here is the main code:

#include "movieType.h"
#include <iostream>
#include <fstream>
#include <string>
#include "PQType.h"
//typedef movieType ItemType;
#include "QueType.h"//FIFO queue 

using namespace std;

int main()
{
	movieType movie[9];
	ifstream inFile("movie1.txt");
	ofstream outFile("printList.txt");

	int i =0;
	bool notDone=true;
	while (notDone) 
	{ 
		if (movie[i++].readMovieInfo(inFile)== false)
			notDone=false;	  
	}

	PQType<movieType> PQqueue(8);

	int j=0;
	while(!PQqueue.IsFull())
	{
		PQqueue.Enqueue(movie[j++]);
	}

	QueType<movieType> fifoQueue[8];
	
	string Genre[8]={"Action", "Comedy", "Drama", "Horror", "Mystery", "Romantic", "Adventure","Western"};
	
	movieType it;
	while (!PQqueue.IsEmpty())
	{
		PQqueue.Dequeue(it);
	
		if (it.getGenre() == 'A')
			fifoQueue[0].Enqueue(it);
		else if (it.getGenre() == 'C' )
			fifoQueue[1].Enqueue(it);
		else if (it.getGenre() == 'D')
			fifoQueue[2].Enqueue(it);
		else if (it.getGenre() == 'H')
			fifoQueue[3].Enqueue(it);
		else if (it.getGenre() == 'M')
			fifoQueue[4].Enqueue(it);
		else if (it.getGenre() == 'R')
			fifoQueue[5].Enqueue(it);
		else if (it.getGenre() == 'V')
			fifoQueue[6].Enqueue(it);
		else if (it.getGenre() == 'W')
			fifoQueue[7].Enqueue(it);
	}

//THE PROBLEM IS GENERATE HERE

	movieType ij;	
	for (int i=0;i<8;++i)
	{
		outFile<<Genre[i]<<" Movies"<<endl;
	
		if (fifoQueue[i].IsEmpty())
			outFile<<"There are no movies for this genre"<<endl;

		for(int j=0; fifoQueue[i].IsEmpty() != true; ++j)
		{
			fifoQueue[i].Dequeue(ij);
			ij.printMovieInfo("printList.txt");
		}

		outFile<<endl;
	}
		return 0;
}

Open in new window


Thanks for the help.
0
Comment
Question by:pgmerLA
3 Comments
 
LVL 14

Assisted Solution

by:sentner
sentner earned 600 total points
ID: 38344036
Instead of passing the name of the file, you should pass a pointer to the file handle object itself.  Attempting to re-open the file within the function is likely where your issue is.
0
 
LVL 8

Accepted Solution

by:
lomo74 earned 1400 total points
ID: 38344513
change movieType::printMovieInfo as follows:
void movieType::printMovieInfo(std::ofstream& outFile)
{
	outFile<<name<<" "<<year<<" "<<genre<<" "<<length<<" "<<rank;
	outFile<<" "<<weight<<" "<<year<<" "<<votes<<std::endl;
	
}

Open in new window

and the call on line 73:
ij.printMovieInfo(outFile);

Open in new window

0
 

Author Comment

by:pgmerLA
ID: 38346237
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question