Question:
C++ PROGRAMMING. PLEASE HELP!!!!?
Stephanie I
2012-04-18 10:13:24 UTC
C++ PROGRAMMING. PLEASE HELP!!!!?
I have asked this question for several days. I am struggling with this program. There is a input file which is of .txt. It contains list of about fifty books in the format: catalogue_number, author_last_name, author_first_name_, book_title, genre, availabilty.
**Catalogue number's are four characters like "A145"
The program needs to store the objects of class book catalogue_number, author_last_name, author_first_name_, book_title, genre, availabilty in an array, sort author_last_name and output as a .txt file.
This is what I have done:
main.cpp: http://pastebin.com/X9BbeUWY
book.cpp: http://pastebin.com/0MADkSVM
book.h: http://pastebin.com/w5w7TuPT

I can't use vector, command line or anything like that. PLEASE!! PLEASE!! PLEASE!! I have been trying for days to make my program work the way it's supposed to work.
Three answers:
TheMadProfessor
2012-04-18 10:32:33 UTC
Just a couple of things that struck me immediately:



1. Your Book class has get methods but no set methods. How do you expect to populate your class variables? You also have defined no constructor for the class (you should have at least one.)

2. You instanciate only a single Book object. What you need is a collection of Book objects and a way to sort them (I suggested an array in one of your earlier postings and using a bubble sort)
husoski
2012-04-18 18:01:36 UTC
This is a skeleton, still. You haven't defined a constructor for your Book class, and you haven't coded any of the input. In short, there's no code to "make work".



You need at least one constructor. C++ will give you a default constructor that takes no arguments, but you have no methods to set private member variables. Just for completeness, and practice, you should have a method to set every value that isn't in the constructor. So far, that is all of them.



Your main code needs a Book[] array. The assignment says "about 50" so give it 100 for now. You also need a count of how many entries in that array are used. This belongs in main.c, not in any Book method.



Book book_list[100];

int book_count = 0;



You need code to read the input text file, one line at a time, and split that into the listed fields: catalog_number, author_last_name, etc. Use these to construct exactly one Book object and store it in your array. Detect if the array is full before storing, though, and fail with an error message.



If you don't know how to split up a string into substrings, and the file contains commas separating those fields, learn. There are a number of ways and your instructor can't expect you to complete the assignment without teaching you at least one. Maybe you're expected to use multiple getline calls:



getline(infile, book_catalogue_number, ',');

getline(infile, author_last_name, ',');

...

getline(infile, availability);



Notice that each getline except the last uses a comma as a delimiter, the last uses a newline to end the "availablity" field. If your data isn't really comma-separated, then how do you handle multiple-word book titles? Pasting a few lines from your textfile could answer this...



Then you need code to sort the array. Like I said, this does NOT belong in a Book method. Book methods know about just one book (*this), and not any array or other collection of books. If you want a list of books as an object, that's another class...save that for later.



Finally, you need a loop to print out the sorted list. Again, in main.c, not in a Book method.
James Bond
2012-04-18 17:46:49 UTC
You need to do a lot. I gave you a program which reads data into an Book type array.



#include

#include

#include

using namespace std;

class Book{

public:



string getCatalogue_Number() const;

string getAuthor_Last_Name() const;

string getAuthor_First_Name() const;

string getBook_Title () const;

string getGenre () const;

string getAvailability () const;

Book(string A, string B, string C, string D, string E, string F)

{

catalogue_number=A;

author_last_name=B;

author_first_name=C;

book_title=D;

genre=E;

availability=F;

}

private:

string catalogue_number;

string author_last_name;

string author_first_name;

string book_title;

string genre;

string availability;

};

string Book::getCatalogue_Number() const{

return catalogue_number;

}

string Book::getAuthor_Last_Name() const{

return author_last_name;

}

string Book::getAuthor_First_Name() const{

return author_first_name;

}

string Book::getBook_Title() const{

return book_title;

}

string Book::getGenre() const{

return genre;

}

string Book::getAvailability() const{

return availability;

}



int main(){

Book **book;

int N=50;

book=(Book **) new Book*[N];

ifstream inStream;

ofstream outStream;

inStream.open("book.cpp"); //dummy file for testing

if(inStream.fail()) {

cout <<"Input file opening failed.\n";

exit(1);

}

outStream.open("computational_task_1.txt");

if(outStream.fail()){

cout <<"Output file opening failed.\n";

exit(1);

}

int i=0;

while(!inStream.eof())

{

string p,q,r,s,t,u;

inStream>>p>>q>>r>>s>>t>>u;

*(book+i)=new Book(p,q,r,s,t,u);

i++;

}

cout<<"Reading from file is over"<
for(i=0;i
cout<getCatalogue_Number()<<"\t"<getAuthor_Last_Name()<


inStream.close();

outStream.close();

system ("PAUSE");

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...