Solved

# Boolean function: give the wrong output

Posted on 2012-08-26
Medium Priority
524 Views
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)
{
getline(inFile,nameMovie);

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

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
{
i++;
}

for (int i=0;i<5;++i)
{
myMovie[i].printMovieInfo("printList.txt");
}

return 0;
}
``````

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

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 Ì
``````

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
0
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))
{
i++;
if (!myMovie[i].readMovieInfo(inFile)) break; // end loop if 'false' is returned
}
``````
0

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++;
``````
0

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)
{
myMovie[k].printMovieInfo("printList.txt");
}
``````
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

## Featured Post

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.
###### Suggested Courses
Course of the Month14 days, 3 hours left to enroll