Home > Enterprise >  My code is outputting an extra line why is this?
My code is outputting an extra line why is this?

Time:02-08

This code I've been working on is so close to being done. However, it keeps printing out an extra line in the output. There is only supposed to be 5 lines in the output but there is six and I can't figure out why this is happening.

This is my book.h file this file cannot be changed in order to complete the code.

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

This is the main.cpp file this file can be changed but should only have code added not taken out.

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

This is the book.cpp file this is where the most change will be done file has some functions but most code will have to be added.

#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();
}

This is the book.txt file that the code is getting its data from.

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

This is what it is currently outputting. The extra line at the end that reads " | Type: Hardback Pages: 176 Weight(lbs): 0.5" should not be there and I can't figure out why it is outputting like that.

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

CodePudding user response:

There's probably a blank line at the end of the data file. Put in

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

just before you construct the book object.

Also as @Eljay said in comments you shouldn't iterate over a file by doing while (!file.eof()): see here.

  •  Tags:  
  • Related