嗨,我正在做一个学校项目,但我在使用特定功能时遇到了困难。我已经为此工作了一段时间,我将不胜感激任何类型的输入。
我们必须使用这个功能:
bool movieLibrary::readMovieInfo(ifstream& inFile)
{
inFile>>rank>>year>>votes>>nationality;
getline(inFile,nameMovie);
if (rank < 1)
return false;
else
return true;
}
我的主要功能不断给出错误的输出:
#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;
}
这是输出,应该与输入相同,但这是我得到的:
3 2000 24446 b Snatch
2 2008 1902 b RocknRolla
5 2007 25510 a American Gangster
-1 -858993460 -858993460 Ì
-858993460 -858993460 -858993460 Ì
这是输入: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
这是 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
};
和 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;
}