简体   繁体   中英

Boolean function doesn't read all the input from a text file

Hi I am working on a school project and I am having a hard time with a particular function. I have been working on it for a while, I would appreciate any type of input.

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;
}

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;
}

Here is the output,which 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 Ì 

Here is the input: myMovieLibrary.txt

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

Here is the MovieLibrary specification file:

#include <string>

class movieLibrary
{
public:
movieLibrary();
~movieLibrary();

//void readMovieInfo(std::ifstream&);
bool readMovieInfo(std::ifstream&);
void printMovieInfo(char*);
char getNationality();
int getRank();
bool operator>=(movieLibrary) const;
bool operator<(movieLibrary) const;

private:
int rank; //rank I gave to the movie in my library
int year; //year the movie came out
int votes; //the number of votes that yahoo users gave the movie
std::string nameMovie; //the name of the movie
char nationality; //nationality of movie: b for british and a for american
};

and the implementation class for MovieLibrary:

#include "movieLibrary.h"
#include <fstream>
#include <string>

using namespace std; // here you can use that.


movieLibrary::movieLibrary()
{
}

movieLibrary::~movieLibrary()
{
}

bool movieLibrary::readMovieInfo(ifstream& inFile)
{
    inFile>>rank>>year>>votes>>nationality;
    getline(inFile,nameMovie);

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

void movieLibrary::printMovieInfo(char* outFileName)
{
std::ofstream outFile;
if(!outFile.is_open()) 
    outFile.open(outFileName, std::ios::app);
outFile<<rank<<" "<<year<<" "<<votes<<" "<<nationality<<" "<<nameMovie<<std::endl;

}
int movieLibrary::getRank()
{
return rank;
}

char movieLibrary::getNationality()
{
return nationality;
}
while (myMovie[i].readMovieInfo(inFile))
{       
    i++;
    myMovie[i].readMovieInfo(inFile);   
}

This code executes this:

myMovie[0].readMovieInfo(inFile); // loads Snatch into [0]
myMovie[1].readMovieInfo(inFile); // loads Layer Cake into [1]
myMovie[1].readMovieInfo(inFile); // loads RocknRolla into [1] 
myMovie[2].readMovieInfo(inFile); // loads Lock,Stock.. into [2]
myMovie[2].readMovieInfo(inFile); // loads Armerican Gangster into [2] 
// until it returns false

This duplicate call of readMoveInfo is what's causing your program to overwrite every 2nd line, eg myMovie[1] will first contain 'Layer Cake' but that will be overwritten with 'RocknRolla' after the function was executed in while() . Easiest solve, as another answer already poined out, is just deleting the 2nd call of readMovieInfo, leaving this:

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

Since you call readMovieInfo in your loop condition, and also in the body of your loop, you'll call it twice for each index except 0.

Since you increment i before the second call in each iteration, you'll overwrite the second element with the third, the fourth with the fifth, and so on: you'll lose every other movie from the input file. This is reflected in your output: the second and fourth movies disappear.

Remember that your loop condition is tested at each iteration , so you need to be careful of any side-effects from the condition test.

You could address this by removing one or the other calls to readMovieInfo (as an exercise try to do it both ways), but I'd prefer to see you write your while condition without calling readMovieInfo ; once you've done this, consider whether a do/while loop might be better, and why.

Finally, note that you should also check that you're not reading more elements than your array can hold. When you do this, your loop condition becomes a more complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM