"getline" error

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 805 Views - Last Post: 15 January 2012 - 06:13 PM Rate Topic: -----

#1 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

"getline" error

Posted 15 January 2012 - 02:50 PM

The code compiles fine but when it runs, it gets all stopped up on the "getline" commands. I displayed "checkpoint" statements to test this as well as commenting out the getline commands. They are definitely what is causing the program to freeze up...my question is why, and how can I fix it?

My code is below; keep in mind it's not yet complete. For example, I still need to finish filling in the switch statement at the bottom. That shouldn't be affecting my current issue, however.

Attached is the .txt file necessary to run this application.

If any more information is required, please let me know. I'll be checking back often for responses. Thank you very much for any help/suggestions you are able to give. :)


#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int MAX_BOOKS = 100;

class bookType
{
		
	private:
		string title; //item-name (some authors are included in this)
		static const int numAuthors = 4;
		string authors[numAuthors]; //authors
		string publisher; //market
		string ISBN; //product_id
		string copies; //quantity
		int i; //for searching by index (of array)

		string item_name[MAX_BOOKS];
		string author1[MAX_BOOKS];
		string author2[MAX_BOOKS];
		string author3[MAX_BOOKS];
		string author4[MAX_BOOKS];
		string quantity[MAX_BOOKS];
		string price[MAX_BOOKS];
		string product_id[MAX_BOOKS];
		string market[MAX_BOOKS];
		string book_header_line;

	public:
           
        void mainDisplay();

		void setTitle();
		void searchTitle();

		void setAuthors();
		void searchAuthors();

		void setPublisher();
		void searchPublisher();

		void setISBN();
		void searchISBN();

		void setPrice();

		void setCopies();

} myBooks[MAX_BOOKS];


int main()
{
    ifstream inFile;
	inFile.open("books.txt");

	if(!inFile)
	{
		cout << "Unable to open input book file!" << endl ;
		system ("pause");
    }
	
	else
	{
        myBooks[MAX_BOOKS].mainDisplay();
    }

	return 0;
}


void bookType::mainDisplay()
{

    ifstream inFile;
    ofstream outFile;

    getline(inFile, book_header_line, '\n');
	cout << book_header_line << endl;

    i = 0;
    cout << "test check 1" << endl;
    system("Pause");
	while(!inFile.eof() && i < MAX_BOOKS)
	{ 
        system("CLS");
	    
		getline(inFile, item_name[i], '\t');
		getline(inFile, author1[i], '\t');
		getline(inFile, author2[i], '\t');
		getline(inFile, author3[i], '\t');
		getline(inFile, author4[i], '\t');
		getline(inFile, price[i], '\t');
        getline(inFile, quantity[i], '\t');
        getline(inFile, product_id[i], '\t');
        getline(inFile, market[i], '\n');
		cout << "\n Book " << i + 1 << 
                "\t Title: "    << item_name[i] <<
                "\t Author 1: " << author1[i] <<
                "\t Author 2: " << author2[i] <<
                "\t Author 2: " << author3[i] <<
                "\t Author 4: " << author4[i] <<
                "\t Publisher: " << market[i] <<
                "\t ISBN: " << product_id[i] <<
                "\t Price: " << price[i] <<
                "\t Quantity: " << quantity[i] << endl;
		
		cout << "\n\n Continue in order, modify data, or perform a search?"
    		    "\n\t 1) Next"
    		    "\n\t 2) Previous"
    		    
    		    "\n\n Modification Options:"
    		    "\n\t 3) Modify Title"
    		    "\n\t 4) Modify Author(s)"
    		    "\n\t 5) Modify Publisher"
    		    "\n\t 6) Modify Price"
    		    "\n\t 7) Modify Quantity"
    		    
    		    "\n\n Search Options:"
    			"\n\t 8) Search by i" 
                "\n\t 9) Search by Title"
                "\n\t10) Search by Author"
                "\n\t11) Search by Publisher"
                
                "\n 12) Exit" << endl;
    
    	int choice;
    
    	do 
    	{
    		cin >> choice;
    		switch (choice)
    		{
    			case 1:
    				{
						i++;
						myBooks[MAX_BOOKS].mainDisplay();
    					break;
    				}
    
    			case 2:
    				{
						i--;
						myBooks[MAX_BOOKS].mainDisplay();
    					break;
    				}
    
    			case 3:
    				{
						
    					break;
    				}
    
    			case 4:
    				{
						
    					break;
    				}
    
    			case 5:
    				{
						
    					break;
    				}
    
    			case 6:
    				{
						
    					break;
    				}
    
    			case 7:
    				{
						
    					break;
    				}
    
    			case 8:
    				{
						
    					break;
    				}
    
    			case 9:
    				{
						
    					break;
    				}
    
    			case 10:
    				{
						
    					break;
    				}
    
    			case 11:
    				{
						
    					break;
    				}
    
    			case 12:
    				{
						
    					break;
    				}
    
    			default:
    				{
    					cout << "Error: Invalid input." << endl;
    					break;
    				}
    		}
    	}
    	while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 || choice != 8 || choice != 9 || choice != 10 || choice != 11 != 12);

	}
	//end while
} 
//end mainDisplay();



Oh also, the class was provided with the following code as an example of how to read the necessary components of the .txt file.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

#define MAX_BOOKS 500

int main()
{
	ifstream inFile;
	ofstream outFile;
    string item_name [MAX_BOOKS];
    string listing_id [MAX_BOOKS];
    string seller_sku [MAX_BOOKS];
    string price [MAX_BOOKS];
    string quantity [MAX_BOOKS];
    string open_date [MAX_BOOKS];
    string item_note [MAX_BOOKS];
    string item_condition [MAX_BOOKS];
    string product_id [MAX_BOOKS];
    string market [MAX_BOOKS];
    string book_header_line;
    int i;
    double tPrice;
    
	inFile.open("books.txt");
	if(!inFile)
		{
		cout << "Unable to open input book file!" << endl ;
		system ("pause");
		return 1;
		}
	
	getline(inFile,book_header_line, '\n');
	cout << book_header_line << endl;
	i = 0;
 	while(!inFile.eof() && i < MAX_BOOKS)
	{
        getline(inFile,item_name[i], '\t');
        getline(inFile,listing_id[i], '\t');
        getline(inFile,seller_sku[i], '\t');
        getline(inFile,price[i], '\t');
        getline(inFile,quantity[i], '\t');
        getline(inFile,open_date[i], '\t');
        getline(inFile,item_note[i], '\t');
        getline(inFile,item_condition[i], '\t');
        getline(inFile,product_id[i], '\t');
        getline(inFile,market[i], '\n');
        tPrice = atof(price[i].c_str());
        cout << item_name[i] << '\t' << listing_id[i] << '\t' << seller_sku[i] << '\t' << price[i] << '\t' << tPrice << '\t' << quantity[i] << '\t' << open_date[i] << '\t' << item_note[i] << '\t' << item_condition[i] << '\t' << product_id[i] << '\t' << market[i] << endl;
        i++;
	}
	system("pause");
	return 0;
}  



Triple post but, the .txt file can be copy/pasted in Microsoft Excel/Open Office Calc for easier viewing, if you are curious as to the contents of the file.

Attached File(s)

  • Attached File  books.txt (16.93K)
    Number of downloads: 26


Is This A Good Question/Topic? 0
  • +

Replies To: "getline" error

#2 The Adrian  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 35
  • Joined: 09-January 12

Re: "getline" error

Posted 15 January 2012 - 03:12 PM

what I would suggest is first open up the ifstream to some file.

Next, line 53 your declaring an array of booktypes of size MAX_BOOKS and in line 69 your indexing into the array at index MAX_BOOKS. Since arrays in c and c++ are 0 based, MAX_BOOKS is one past the last element which is wrong.

I think that should solve a few of your problems

Sorry not 69, 82
Was This Post Helpful? 0
  • +
  • -

#3 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 03:30 PM

Quote

What I would suggest is first open up the ifstream to some file.

What do you mean by this?

Also,
Line 82 is: "getline(inFile, book_header_line, '\n');"

MAX_BOOKS is not even used on this line.

I do understand that MAX_BOOKS is set to 100 and the array is declared as such; therefore the indices of the array are 0-99. Another issue is the fact that the text list has 101 lines in it, 100 of which are books. The textbook instructs us to "declare an array of 100 components of type bookType," yet the professor gave us a list with 101 lines in it (the first is a "header" line). I was wondering if that was going to cause an issue at any point throughout the program.

forgot the / in the quote tags, sorry. Can't seem to edit my posts (yet?).
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: "getline" error

Posted 15 January 2012 - 03:34 PM

There is an error in the condition for your do-while loop at the end of main.
while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 || choice != 8 || choice != 9 || choice != 10 || choice != 11 != 12);

At the very end of the condition you have "choice != 11 != 12", which is an error.

You could make the conditional alot shorter and easier to read by just using boolean operators and a range of numbers, like so:
while(choice < 1 || choice > 12);


This would read like "loop while choice is less than 1 OR choice is greater than 12. It does the same thing as your while loop but it's shorter and easier to read.

This post has been edited by vividexstance: 15 January 2012 - 03:34 PM

Was This Post Helpful? 1
  • +
  • -

#5 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 03:36 PM

Also, there's a small error in the do-while loop, I've patched it in my own code but cannot edit it above. It was not the source of the problem, however.

View Postvividexstance, on 15 January 2012 - 03:34 PM, said:

There is an error in the condition for your do-while loop at the end of main.
while (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7 || choice != 8 || choice != 9 || choice != 10 || choice != 11 != 12);

At the very end of the condition you have "choice != 11 != 12", which is an error.

You could make the conditional alot shorter and easier to read by just using boolean operators and a range of numbers, like so:
while(choice < 1 || choice > 12);


This would read like "loop while choice is less than 1 OR choice is greater than 12. It does the same thing as your while loop but it's shorter and easier to read.


You caught that just as I did, haha. Also, that is a wonderful suggestion. I'll be sure to change that immediately. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: "getline" error

Posted 15 January 2012 - 03:38 PM

The problem, like The Adrian said, is that you try to access:
myBooks[MAX_BOOKS].mainDisplay();

This is an error because array indexing in C/C++ starts at zero not one, so the array is from 0 to MAX_BOOKS-1.
Was This Post Helpful? 1
  • +
  • -

#7 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 03:48 PM

Oh, ok. So it was 69. I've changed it to MAX_BOOKS-1; it now bypasses the getline statement fine. However, it does not seem to enter the while loop.

while(!inFile.eof() && i < MAX_BOOKS)
{
   ...
}


I commented this loop out and it enters the loop fine, however does not display any of the information. I have no knowledge of working with reading data from a file outside of the code he provided us; this was optional to prevent us from having to input 100 books' titles, authors, prices, etc. manually. Any suggestions regarding this issue?

The current code is as follows (I've been adding to it as well):

//#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int MAX_BOOKS = 100;

class bookType
{
		
	private:
		string title; //item-name (some authors are included in this)
		static const int numAuthors = 4;
		string authors[numAuthors]; //authors
		string publisher; //market
		string ISBN; //product_id
		string copies; //quantity
		int i; //for searching by index (of array)

		string item_name[MAX_BOOKS];
		string author1[MAX_BOOKS];
		string author2[MAX_BOOKS];
		string author3[MAX_BOOKS];
		string author4[MAX_BOOKS];
		string quantity[MAX_BOOKS];
		string price[MAX_BOOKS];
		string product_id[MAX_BOOKS];
		string market[MAX_BOOKS];
		string book_header_line;

	public:
           
        void mainDisplay();

		void setTitle();
		void searchTitle();

		void setAuthors();
		void searchAuthors();

		void setPublisher();
		void searchPublisher();

		void setISBN();
		void searchISBN();

		void setPrice();

		void setQuantity();

} myBooks[MAX_BOOKS];


int main()
{
    ifstream inFile;
	inFile.open("books.txt");

	if(!inFile)
	{
		cout << "Unable to open input book file!" << endl ;
		system ("pause");
    }
	
	else
	{
        myBooks[MAX_BOOKS-1].mainDisplay();
    }

	return 0;
}


void bookType::mainDisplay()
{
    ifstream inFile;
    ofstream outFile;

    getline(inFile, book_header_line, '\n');
	cout << book_header_line << endl;
	
//	while(!inFile.eof() && i < MAX_BOOKS)
	//{ 

        system("CLS");
	    
		getline(inFile, item_name[i], '\t');
		getline(inFile, author1[i], '\t');
		getline(inFile, author2[i], '\t');
		getline(inFile, author3[i], '\t');
		getline(inFile, author4[i], '\t');
		getline(inFile, price[i], '\t');
        getline(inFile, quantity[i], '\t');
        getline(inFile, product_id[i], '\t');
        getline(inFile, market[i], '\n');
		cout << "\n Book " << i + 1 << 
                "\n Title: "    << item_name[i] <<
                "\n Author 1: " << author1[i] <<
                "\n Author 2: " << author2[i] <<
                "\n Author 3: " << author3[i] <<
                "\n Author 4: " << author4[i] <<
                "\n Publisher: " << market[i] <<
                "\n ISBN: " << product_id[i] <<
                "\n Price: " << price[i] <<
                "\n Quantity: " << quantity[i] << endl;
		
		cout << "\n\n Continue in order, modify data, or perform a search?"
    		    "\n\t 1) Next"
    		    "\n\t 2) Previous"
    		    
    		    "\n\n Modification Options:"
    		    "\n\t 3) Modify Title"
    		    "\n\t 4) Modify Author(s)"
    		    "\n\t 5) Modify Publisher"
    		    "\n\t 6) Modify ISBN"
    		    "\n\t 7) Modify Price"
    		    "\n\t 8) Modify Quantity"
    		    
    		    "\n\n Search Options:"
    			"\n\t 9) Search by Index" 
                "\n\t10) Search by Title"
                "\n\t11) Search by Author"
                "\n\t12) Search by Publisher"
                "\n\t13) Search by ISBN"
                
                "\n\n\t14) Exit" << endl;
    
    	int choice;
    
    	do 
    	{
    		cin >> choice;
    		switch (choice)
    		{
    			case 1:
    				{
						i++;
						myBooks[MAX_BOOKS-1].mainDisplay();
    					break;
    				}
    
    			case 2:
    				{
						i--;
						myBooks[MAX_BOOKS-1].mainDisplay();
    					break;
    				}
    
    			case 3:
    				{
						myBooks[MAX_BOOKS-1].setTitle();
    					break;
    				}
    
    			case 4:
    				{
						myBooks[MAX_BOOKS-1].setAuthors();
    					break;
    				}
    
    			case 5:
    				{
						myBooks[MAX_BOOKS-1].setPublisher();
    					break;
    				}
    
    			case 6:
    				{
						myBooks[MAX_BOOKS-1].setISBN();
    					break;
    				}
    
    			case 7:
    				{
						myBooks[MAX_BOOKS-1].setPrice();
    					break;
    				}
    
    			case 8:
    				{
						myBooks[MAX_BOOKS-1].setQuantity();
    					break;
    				}
    
    			case 9:
    				{
                        //myBooks[MAX_BOOKS-1].searchIndex();
						cout << "\n Enter new index: ";
						cin >> i;
						myBooks[MAX_BOOKS-1].mainDisplay();
    					break;
    				}
    
    			case 10:
    				{
						myBooks[MAX_BOOKS-1].searchTitle();
    					break;
    				}
    
    			case 11:
    				{
						myBooks[MAX_BOOKS-1].searchAuthors();
    					break;
    				}
    
    			case 12:
    				{
						myBooks[MAX_BOOKS-1].searchPublisher();
    					break;
    				}
    
    			case 13:
    				{
						myBooks[MAX_BOOKS-1].searchISBN();
    					break;
    				}
    
    			case 14:
    				{
						system("Exit");
    					break;
    				}
    
    			default:
    				{
    					cout << "Error: Invalid input." << endl;
    					break;
    				}
    		}
    	}
    	while (choice < 1 || choice > 14);

//	}
	//end while
} //end mainDisplay();


//************ Modify/Set functions below this line ************

void bookType::setTitle()
{
     
}


void bookType::setAuthors()
{
     
}


void bookType::setPublisher()
{
     
}


void bookType::setISBN()
{
     
}


void bookType::setPrice()
{
     
}


void bookType::setQuantity()
{
     
}


//************ Search functions below this line ************

void bookType::searchTitle()
{
     
}


void bookType::searchAuthors()
{
     
}


void bookType::searchPublisher()
{
     
}


void bookType::searchISBN()
{
     
}


Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: "getline" error

Posted 15 January 2012 - 03:55 PM

Why are you using the last element? Shouldn't you be using the 'i' class member to index the myBooks array?

*NOTE*: You should be checking to make sure that 'i' contains a correct index into the array whenever you are trying to access the array.

This post has been edited by vividexstance: 15 January 2012 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 04:01 PM

Mainly because I've no idea what I'm doing to be honest. My only experience is what I can glean from the book and various web sites, as it is an online class. I'm essentially trying to teach this to myself and have been frustrated over it for the past 3 days, so I finally came here seeking help.

I replaced MyBooks-1 with i and was told that was undeclared (I'm guessing since i is a member variable of the class bookType). To check if it would make a difference, I set i as a global variable (which I should apparently avoid doing?); this compiled properly. After running, I still am not seeing any information from the .txt file. :(
Was This Post Helpful? 0
  • +
  • -

#10 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 04:12 PM

replaced "MyBooks-1" should say "MAX_BOOKS"


Anyway, I'm trying to make sense of what I'm attempting to do here, one step at a time below...if something is incorrect, please point it out.


myBooks[MAX_BOOKS] is an array of 100 objects, indexed from 0-99.

The line myBooks[i].mainDisplay(); is calling the member function "mainDisplay()."

What is the "myBooks[i]" part for? I understand it's necessary, but I don't know why or what it's really doing. I know if I were to say myBooks[i].Title = "Title of the book" it would be setting the title of the myBooks object located at index i (whatever that may be at the time) to what is in the parenthesis. But, why is referencing an object within the array necessary to call a function?

quotation marks, not parenthesis...really need an edit button. :censored:
Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: "getline" error

Posted 15 January 2012 - 04:17 PM

I was talking about using 'i' inside the member function mainDisplay(). You definitely should avoid using global variables, so leave the 'i' inside the class. That way 'i' can't be changed by anything but inside of the class.

The way your class is designed and the way you are trying to use it is wrong IMHO. You have a class that holds information for many books. You then create a global array of that class. Inside of the class' implementation, you use that array.

I would make a class just to hold the information for a single book. Then make a class that holds an array of the class of books. That class will do all of the input/output of the books.

This post has been edited by vividexstance: 15 January 2012 - 04:18 PM

Was This Post Helpful? 1
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: "getline" error

Posted 15 January 2012 - 04:23 PM

Your mainDisplay function doesn't really make sense. Every other aspect of your bookType class represents characteristics of a single book. So any "display" function that's a member of the bookType class should deal with just 1 book, for example, to display information specific to that particular book.

You're creating an array of bookType objects. The mainDisplay function that reads the input file, loads that array, and iterates through that array to search for titles, etc., should be a global function, not a member of the bookType class. The array of books, on the other hand, should not be global. That array should be declared inside the main function, and it should be passed to the mainDisplay (global) function as a parameter.

edit: yes, as vividexstance said. Except for the i. That has no business being a class member. And neither do any of those arrays that you have inside the class. A single book only has a single name, not an array of 100 names. Same goes for all the other data fields.

This post has been edited by r.stiltskin: 15 January 2012 - 04:27 PM

Was This Post Helpful? 1
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,401
  • Joined: 19-February 09

Re: "getline" error

Posted 15 January 2012 - 04:35 PM

View PostCode Noob, on 16 January 2012 - 01:12 AM, said:

myBooks[MAX_BOOKS] is an array of 100 objects, indexed from 0-99.


Yes, an array of 100 books.


View PostCode Noob, on 16 January 2012 - 01:12 AM, said:

The line myBooks[i].mainDisplay(); is calling the member function "mainDisplay()."

What is the "myBooks[i]" part for? I understand it's necessary, but I don't know why or what it's really doing. I know if I were to say myBooks[i].Title = "Title of the book" it would be setting the title of the myBooks object located at index i (whatever that may be at the time) to what is in the parenthesis. But, why is referencing an object within the array necessary to call a function?


Not just any function, the function was created as a member of the class bookType, so needs a class object. Normal functions don't need an object of a class.


View PostCode Noob, on 16 January 2012 - 12:30 AM, said:

... Another issue is the fact that the text list has 101 lines in it, 100 of which are books. The textbook instructs us to "declare an array of 100 components of type bookType," yet the professor gave us a list with 101 lines in it (the first is a "header" line). I was wondering if that was going to cause an issue at any point throughout the program.


The header line can be discarded.

This post has been edited by #define: 15 January 2012 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#14 Code Noob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: "getline" error

Posted 15 January 2012 - 04:39 PM

By doing that, wouldn't I be disobeying the instructions?

part 1A: Design a class "bookType" that defines the book as an ADT. (ADT is abstract data type, though I don't really understand what that means...possibly the biggest issue here?)

part 1B: Each object of the class can hold the following info. about the book: title, up to 4 authors, publisher, ISBN, price, and quantity. To keep track of the number of authors, add another member variable

part 1C: Include the member functions to perform the various operations on objects of type bookType. (Examples of operations here).

part 2A: Write the definitions of the member functions of the class bookType.

part 3A: Write a program that uses the class bookType and tests various operations on the objects of the class bookType. Declare an array of 100 components [b]of type bookType[b]. Some operations include: (example operations here).

I've been struggling with this assignment for 3 days now. I have asked the professor questions, did not understand his answers (much like what is happening here, to a lesser extent), nor were they quick at all. The program is due tonight and I am getting more and more frustrated as I spend literally every minute of my free time on this one homework problem. I have already solved numerous others in the same chapter with little to problems; not sure why this one is such a bugger for me.

Part of the problem, I guess, is that I really don't understand most of what it's asking me to do. I could create this program with ease, though not in the way it's asking me to do so. The code would be rudimentary, but it would work. However, this is not the assignment, and I'm trying to learn this as best as I can. :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: "getline" error

Posted 15 January 2012 - 05:13 PM

Basically your problem is that you have a class that is supposed to hold info for 1 (one) book. You then make a global array of that class and you use that array inside the member functions for the book class. This is the problem. That is called poor design.

Like I said before, just make a class that holds information for a single book. You can then either write code in main() or create another class to deal with the inputting and outputting of the books. If you do the latter, you would then just use the class for I/O of books in main. Example:
class Book
{
    string name;
    string author;
    int isbn;
public:
    Book();
    // member functions to view/edit a single book
};

#define MAX_BOOKS 100

class Bookshelf
{
    Book books[MAX_BOOKS];
public:
    Bookshelf();
    // member functions for inputting/outputting books

    // Takes a filename as a string argument and opens that file.  Then it reads the books into the books array.
    void readFile(const string& fname);
    // Prints the books array to the screen.
    void displayBooks() const;
};

int main()
{
    Bookshelf bs;
    bs.readfile("books.txt");
    bs.displayBooks();
}


*NOTE*: I did not compile this program, it's not complete, I was just showing you what I was talking about. This would be a more reasonable design choice.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2