C++ Difficulty with File Input Loop?

Hello! I'm a very novice programmer and I'm having trouble with File input looping.

I'm reading from a list of books and their associated titles, cover types, pages, and weights. I've currently spent over an hour trying to figure out how to write this so that the loop continues on to the next grouping of data(title followed by cover type followed by page count, and ending on weight).

It currently prints out the first line correctly, then infinitely loops the first book's cover type, pages, and weight without the title. I've been spending a long time trying to figure this one out. Any help would be immensely appreciated, Thank you!

The text file contains the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   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 

Here is my main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   using namespace std;
    
    int main()
    {
    	const string FILENAME("books.txt");
       
    	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');  
    			
    			Book newBook(name, static_cast<Type>(type), pages, ounces);
    
    			cout << newBook.formatReportLine() << endl;	
    		}
    	}
    	else
    	{
    		cout << "File not found: " << FILENAME << endl;
    	}
       
    	return 0;
    }

Here is Book.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    //Constructor
    Book::Book( const string& name, Type type, int pages, float ounces )
    {
    	bName = static_cast<string>(name);
    	bType = type;
    	bPages = pages;
    	bOunces = ounces;
    }
    
    //Default Constructor
    Book::Book() {
    	bName = "";
    	bType = UNKNOWN;
    	bPages = 0;
    	bOunces = 0.0f;
    }
    
    float Book::getWeightLbs()
    {
    	const float OUNCES = 16.0f;
    	return bOunces / OUNCES;
    }
    
    string Book::getTypeName()
    {
    	return TYPE_WORDS[bType];
    }
    
    string Book::formatReportLine() 
    {
    	stringstream reportLine;
    	reportLine << Book::getName() << setw(10) << "| Type: " << Book::getTypeName() 
<< setw(10) << "Pages: " << Book::getPages()<< setw(10) << "Weight: " << Book::getWeightLbs(); 
    	
    	return reportLine.str();
    }


And finally book.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    using namespace std;
    
    enum Type
    {
    	UNKNOWN = -1,
       PAPERBACK,
    	HARDBACK
    };
    
    const string TYPE_WORDS[] = { "Paperback", "Hardback" };
    
    class Book
    {
    public:
    	Book();
    	//constructor
    	Book( const string& name, Type type, int pages, float ounces );
    	//destructor
    	~Book(){};
       
    	string formatReportLine();  
    	float getWeightLbs();  
    	string getTypeName();  
       
    	//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 
    	int bPages;  //how many pages the book contains
    	float bOunces;  //how much the book weighs in ounces
    };
Hmm, I copied your code all into one file, and the only changes I made was adding header files. I compiled and ran it, and this is what I got:

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


Looks like it's working as you intended on my system (pretty standard Linux setup), so maybe consider anything special in your environment.

$ uname -a
Linux ubuntu 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:23:46 UTC 2014 i686 i686 i686 GNU/Linux
$ g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
...
Topic archived. No new replies allowed.