Boolean function: give the wrong output

Posted on 2012-08-26
Medium Priority
Last Modified: 2012-08-28
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.
Question by:pgmerLA
  • 2
LVL 86

Expert Comment

ID: 38334682
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

LVL 86

Accepted Solution

jkr earned 1000 total points
ID: 38334687
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

LVL 32

Assisted Solution

phoffric earned 1000 total points
ID: 38335001
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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 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