Boolean function: give the wrong output

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)

		if (rank < 1)
			return false;
			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))
	for (int i=0;i<5;++i)

	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
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.
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

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

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

Open in new window

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)

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.
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.