Boolean function: give the wrong output

Hi,
I am working on a school project and I am having a hard time with a particular function.
We have to use this function:
bool movieLibrary::readMovieInfo(ifstream& inFile)
{
		inFile>>rank>>year>>votes>>nationality;
		getline(inFile,nameMovie);

		if (rank < 1)
			return false;
		else
			return true;
}

Open in new window


My main function keeps giving the wrong output:
#include "movieLibrary.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	movieLibrary myMovie[5];
	ifstream inFile("myMovieLibrary.txt");

	int i =0;

	//my issue is here
	while (myMovie[i].readMovieInfo(inFile))
	{		
		i++;
		myMovie[i].readMovieInfo(inFile);	
	}
	
	for (int i=0;i<5;++i)
	{
		myMovie[i].printMovieInfo("printList.txt");
	}

	return 0;
}

Open in new window


Here is the input:
3 2000 24446 b Snatch
2 2004 2872 b Layer Cake
2 2008 1902 b RocknRolla
4 1999 7661 b Lock,Stock and Two Smoking Barrels
5 2007 25510 a American Gangster
-1
rank year votes Nationality (b:british; a:american) name

Open in new window


and the output should be the same as the input, but here is what I am getting:
3 2000 24446 b  Snatch
2 2008 1902 b  RocknRolla
5 2007 25510 a  American Gangster
-1 -858993460 -858993460 Ì 
-858993460 -858993460 -858993460 Ì 

Open in new window


I have been working on it for a while, I would appreciate any type of input.
Thanks!
main.cpp
movieLibrary.cpp
movieLibrary.h
myMovieLibrary.txt
printList.txt
pgmerLAAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Sorry, disregard my last comment, the second call in the loop body is causing that, omit it - then the loop should be

	while (myMovie[i].readMovieInfo(inFile)) i++;

Open in new window

0
 
jkrCommented:
You are not evaluating the return value at all, just make that

	while (myMovie[i].readMovieInfo(inFile))
	{		
		i++;
		if (!myMovie[i].readMovieInfo(inFile)) break; // end loop if 'false' is returned	
	}

Open in new window

0
 
phoffricConnect With a Mentor Commented:
First, a style comment. Your first while-loop should be a for-loop since it follows the for-loop idiom very well. Your while-loop is computing i, which you ignore in your next for-loop. Here is how you can use it:
	for (int i=0; myMovie[i].readMovieInfo(inFile) == true; ++i);
	
	for (int k=0;k<i;++k)
	{
		myMovie[k].printMovieInfo("printList.txt");
	}

Open in new window

A second style comment is to have test conditions explicitly indicate the condition; in this case, you are testing the output to be true.
A third style comment is to modify the i counter to represent what it is counting; in this case, perhaps, numberOfMovies might be a better substitution for i. You can give all the points to jkr.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.