Boolean function: give the wrong output

Posted on 2012-08-26
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
    LVL 86

    Expert Comment

    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

    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 31

    Assisted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now