简体   繁体   English

布尔函数无法从文本文件中读取所有输入

[英]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 输入如下: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: 这是MovieLibrary规范文件:

#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: 以及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() . 重复调用readMoveInfo是导致您的程序每隔第二行覆盖一次,例如myMovie [1]将首先包含“ Layer Cake”,但在while()执行该函数后,它将被“ RocknRolla”覆盖。 Easiest solve, as another answer already poined out, is just deleting the 2nd call of readMovieInfo, leaving this: 最简单的解决方法是删除第二个readMovieInfo调用,这是已经回答的另一个答案,剩下的是:

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. 由于您在循环条件中以及循环体中都调用readMovieInfo ,因此对于每个索引(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. 由于您在每次迭代中在第二次调用之前增加i ,因此您将用第三个元素覆盖第二个元素,用第五个元素覆盖第四个元素,依此类推:您将丢失输入文件中的所有其他电影。 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 ; 您可以通过删除对readMovieInfo一个或另一个调用来解决此readMovieInfo (作为一种练习,尝试两种方式都做),但是我更希望看到您while不调用readMovieInfo情况下编写了while条件; once you've done this, consider whether a do/while loop might be better, and why. 完成此操作后,请考虑执行do / while循环是否会更好,以及原因。

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. 执行此操作时,循环条件将变得更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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