|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (7 bit)
Join Date: May 2004
Posts: 103
|
Code help
I am working on a computer science project and am stuck. THe program is written in C++ and has three classess. Below is the code for the three classess. The problem I have is with line 13 - Library theLibrary(card, book );
After I compile the program it seg faultls with this line. The program will print "line 12" but if I put in a cout statement as the first line of the constructor, it will not get printed. What am I doing wrong? Any help is greatly appreciated. Thanks in advance, Shaticus THe main: // File: proj3.cc // main function for library app #include #include #include "library.h" int main() { char book[] = {"books.dat"}; char card[] = {"cards.dat"}; cout << "Line 12"; Library theLibrary(card, book ); cout << "Line 14"; char Command; bool do_exit = false; do { cout << theLibrary.showMenu(); cin >>Command; do_exit = theLibrary.DoCommand( Command ); } while ( do_exit == false ); return 0; } The Library Class: #include "book.h" #include "card.h" #include using namespace std; class Library{ public: Library(char[], char[]); char* showMenu(); bool DoCommand(int); private: int totalCards; int totalBooks; Book books[100]; Card cards[100]; }; Library::Library(char bookStorage[], char cardStorage[]) { int i = 0; ifstream BookFile(bookStorage); if(!BookFile) { cout << "Error"; exit(-1); } else { while(BookFile >> books[i] != NULL) i++; i = 0; } ifstream CardFile(cardStorage); if(!CardFile) { cout << "Error Opening FIle"; exit(-1); } return; } char* Library::showMenu() { static char menu[218] = {"Welcome to the library management system!\nPlease enter your choice:\n1: show all card holders in the library\n2: show all books in the library\n3: to check in a book\n4: to check out a book\n5: to exit\nEnter your choice: "}; return menu; } bool Library:: DoCommand(int command) { int i = 0; if(command == 1) { for(i = 0; i < totalCards; i++) cout << cards; } if(command == 2) void printBooks(); return false; if(command = 3) // book.setStatus(); return false; if(command == 5) return true; } //edit for code posting problems. Last edited by shaticus; 05-14-2005 at 08:35 AM. |
|
|
|
|
|
#2 |
|
Member (9 bit)
Join Date: Feb 2005
Posts: 392
|
#include < fstream > - You need to include this file in order to use C++’s functions for File I/O.
must you read one char a time into a string? if so, you must use: char a_char; BookFile>>a_char != f_eof() books += a_char; or books[i] = a_char; can you use "getline", it's a read once thing; or may be your file is more then one line, eh? Last edited by alfie2; 05-15-2005 at 03:47 PM. |
|
|
|
|
|
#3 |
|
Member (7 bit)
Join Date: May 2004
Posts: 103
|
Thanks for the reply.
I included fstream in the library header. But the program still seg faults. books is another class I have working correctly. So, books >> book[i] is using operator overloading to read in four lines of data. Thanks, Shaticus |
|
|
|
|
|
#4 |
|
Member (9 bit)
Join Date: Feb 2005
Posts: 392
|
you used Library constructor in MAIN() like so:
Library theLibrary(card, book ); and you declare in your CLASS as and used in your contructor: Library::Library(char bookStorage[], char cardStorage[]) is this a mistype? should it be?: Library theLibrary(book, card); -------------------- also, since "seg fault" means pointer dereferencing problem it could be parameter passing error, errr maybe. |
|
|
|
|
|
#5 |
|
Member (7 bit)
Join Date: May 2004
Posts: 103
|
I figured it out!
I used strcpy in one of the functions in my class but it was not in the cstring header file. It is very strange that this would not cause a compile error. Thanks for all of your help Alfie2! Last edited by shaticus; 05-17-2005 at 06:55 AM. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|