我的代码输出了额外的一行,这是为什么?

My code is outputting an extra line why is this?

我一直在处理的代码即将完成。但是,它一直在输出中打印出额外的一行。输出中应该只有 5 行,但有 6 行,我不明白为什么会这样。

这是我的 book.h 文件,无法更改此文件以完成代码。

#include <string>

using namespace std;

enum Type
{
    UNKNOWN = -1,
   PAPERBACK,
    HARDBACK
};

const string TYPE_WORDS[] = { "Paperback", "Hardback" };

class Book
{
public:
    //default constructor - not actually used but should be implemented anyway
    Book();
    //constructor
    Book( const string& name, Type type, int pages, float ounces );
    //destructor
    ~Book(){};
   
    string formatReportLine();  //return a string with all the info for the book
    float getWeightLbs();  //calculate and return the weight of the book in lbs
    string getTypeName();  //return the string which correlates with the book type
   
    //accessors
    string getName(){ return bName; };
    Type getType(){ return bType; };
    int getPages(){ return bPages; };
    float getOunces(){ return bOunces; };
   
private:
    string bName;  //name of the book
    Type bType;  //the type of book (Type is an enumerated type)
    int bPages;  //how many pages the book contains
    float bOunces;  //how much the book weighs in ounces
};

这是 main.cpp 文件,此文件可以更改,但应该只添加代码而不删除。

#include <iostream>
#include <fstream>
#include <string>
#include "book.h"

using namespace std;

int main()
{
    const string FILENAME("books.txt");
  const int INT_MAX = 1;
   
    ifstream input(FILENAME);
    if( input.good() )
    {
        while( !input.eof() )
        {
            string name;
            int type;
            int pages;
            float ounces;
            getline( input, name );
            input >> type >> pages >> ounces;
            input.ignore(INT_MAX, '\n');  //ignore the newline char at the end of the line
         
            //create Book object here!
      Book myBook(name,(Type)type, pages, ounces);
            //write out report line for book here!
            cout << myBook.formatReportLine();
        }
    }
    else
    {
        cout << "File not found: " << FILENAME << endl;
    }
   
    system("pause");
    return 0;
}

这是 book.cpp 文件,这是将进行最多更改的地方文件具有一些功能,但必须添加大部分代码。

#include <iostream>
#include <fstream>
#include <string>
#include "book.h"
#include "sstream"

Book::Book()
{

}

Book::Book( const string& name, Type type, int pages, float ounces )
{
  bName = name;
  bType = type;
  bPages = pages;
  bOunces = ounces;
}

float Book::getWeightLbs()
{
  float answer;
  answer = bOunces / 16;
  return answer;
}

string Book::getTypeName()
{
  string typeName = "";
  if(bType == PAPERBACK) 
  typeName = TYPE_WORDS[bType];

  else if (bType == HARDBACK)
  typeName = TYPE_WORDS[bType];

  else
  typeName = "Unknown";

  return typeName;
}

string Book::formatReportLine()
{ 
  stringstream sout;
  sout << bName << " | Type: " << getTypeName() << " Pages: " << bPages << " Weight(lbs): " << getWeightLbs() << endl;
  return sout.str();
}

这是代码从中获取数据的 book.txt 文件。

The Human Use of Human Beings
0
200
8.0
Utopia
0
176
4.8
Hackers
0
520
22.4
The Information
1
544
33.6
Sterling's Gold
1
176
8.0

这是它当前输出的内容。末尾的额外行“|类型:精装页:176重量(磅):0.5”不应该在那里,我不明白为什么它会这样输出。

The Human Use of Human Beings | Type: Paperback Pages: 200 Weight(lbs): 0.5
Utopia | Type: Paperback Pages: 176 Weight(lbs): 0.3
Hackers | Type: Paperback Pages: 520 Weight(lbs): 1.4
The Information | Type: Hardback Pages: 544 Weight(lbs): 2.1
Sterling's Gold | Type: Hardback Pages: 176 Weight(lbs): 0.5
 | Type: Hardback Pages: 176 Weight(lbs): 0.5

数据文件末尾可能有一个空行。放入

if (name.empty()) {
    continue;
}

就在您构造图书对象之前。

正如@Eljay 在评论中所说,您不应该通过执行 while (!file.eof()) 来遍历文件:see here.