class variables
I made a class for a library and a class for books in the library but whenever I try to save a variable to an element of an array of book objects in the library class, which is a friend to the book class, I get a runtime error. Here's the line that seems to be giving me problems:
bookArray.bookID = inNum;
Both are ints and I tested inNum, it works fine. I commented the line out and the program ran until it found the next line like that. I must've done something wrong but my textbook doesn't have any examples of arrays of classes so I don't know what I did.
You need to post the class declarations for us to tell you what's wrong. Go ahead and post them in source tags so we can take a look.
When I have runtime errors with arrays, it's because I'm trying to access out of bounds. Example:
That'll cause a runtime error because the program is trying to access the array where it doesn't exist. And, we'll probably be able to help more if you post more code.
int array[5];cout << array[7] << endl;
That'll cause a runtime error because the program is trying to access the array where it doesn't exist. And, we'll probably be able to help more if you post more code.
What programming language are you using? C++? Pascal? Java? etc. What's that runtime error saying?
Anyway, (assuming it's C++, because you mentioned it's friend) are you sure that variable i is correct, so that it's >= 0 and < bookArray length?
There aren't more obvious errors in this piece of code, unless you're using C++ with operator overloading, and operator= is doing sth not trivial...
I'll post the full code, but I forgot the code for source tags. Can someone please remind me?
I don't know if you need the .cpp file but I included it anyway.
#ifndef LIBRARY_H#define LIBRARY_H#include <fstream.h>#include "book.h"#include "card.h"class library{ public: library(int, int, book*, card*); ~library(){} bool DoCommand(char); void showMenu(); private: int numCards, numBooks; book bookArray[]; card cardArray[]; int findBook(book*, int); int findCard(card*, int); void initFiles(book*, card*, int, int); void transact(book*, card*); void addRemBooks(book*); void addRemCards(card*); void logOut(book*, card*, int, int);};library::library(int numCards, int numBooks, book bookArray[], card cardArray[]){ numCards = 0; numBooks = 0; initFiles(bookArray, cardArray, numBooks, numCards);}void library::initFiles(book bookArray[], card cardArray[], int numBooks, int numCards){ int inNum, i = 0; //vars read to be put into objects char* inName; ifstream incard("cards.dat", ios::in); ifstream inbook("books.dat", ios::in); if((incard == NULL) || (inbook == NULL)) { cerr << "One or more files not found."; exit(-1); } do{ inbook >> inNum; bookArray.bookID = inNum; inbook >> inName; bookArray.title = inName; inbook >> inName; bookArray.author = inName; inbook >> inNum; bookArray.cardID = inNum; inbook >> inNum; bookArray.status = inNum; inbook >> inNum; i++; } while (inNum != 2); //use 2 as EOF and 3 if more books numBooks = i; i = 0; do{ incard >> inNum; cardArray.cardID = inNum; incard >> inName; cardArray.name = inName; incard >> inNum; cardArray.phone = inNum; incard >> inNum; cardArray.booksOut = inNum; incard >> inNum; i++; } while (inNum != 2); //use 2 as EOF and 3 if more books numCards = i;}bool library::DoCommand(char command){ int searchID, i; switch(command) { case 'Q': case 'q': logOut(bookArray, cardArray, numBooks, numCards); return false; break; case 'B': case 'b': cout << "Enter Book ID: "; cin >> searchID; i = findBook(bookArray, searchID); //find book element if(i == numBooks) //if book not found, return return true; cout << bookArray.bookID << endl << bookArray.title << endl << bookArray.author << endl << bookArray.cardID << endl << bookArray.status << endl; return true; break; case 'C': case 'c': cout << "Enter Card ID: "; cin >> searchID; i = findCard(cardArray, searchID); //find card element if(i == numCards) //if card not found, return return true; cout << cardArray.name << endl << cardArray.phone << endl << cardArray.booksOut << endl; return true; break; case 'T': case 't': transact(bookArray, cardArray); return true; break; case 'O': case 'o': addRemBooks(bookArray); return true; break; case 'A': case 'a': addRemCards(cardArray); return true; break; default : return true; break; }}void library::logOut(book bookArray[], card cardArray[], int numBooks, int numCards){ ofstream outcard("cards.dat", ios::out); ofstream outbook("books.dat", ios::out); for(int i = 0; i < numBooks; i++) { outbook << bookArray.bookID << " " << bookArray.title << " " << bookArray.author << " " << bookArray.cardID << " " << bookArray.status << " "; if(i = --numBooks) { outbook << "2" << endl; //using 2 as EOF return; } else outbook << "3" << endl; //3 if more objects } for(int i = 0; i < numCards; i++) { outcard << cardArray.cardID << " " << cardArray.name << " " << cardArray.phone << " " << cardArray.booksOut << " "; if(i = --numCards) { outcard << "2" << endl; //using 2 as EOF return; } else outcard << "3" << endl; //3 if more objects }}int library::findBook(book bookArray[], int searchID){ int i; for(i = 0; i < numBooks; i++) { if(bookArray.bookID == searchID) { return i; } } cout << "Book not found." << endl; return i;}int library::findCard(card cardArray[], int searchID){ int i; for(i = 0; i < numCards; i++) { if(cardArray.cardID == searchID) { return i; } } cout << "Card not found." << endl; return i;}void library::transact(book bookArray[], card cardArray[]){ int cardID, bookID, card, book, command; cout << "What do you want to do?" << endl << "1 - Check out book" << endl << "2 - Return book" << endl << "3 - Back" << endl; switch (command) { case 1 : cout << "Enter card ID: "; cin >> cardID; card = findCard(cardArray, cardID); cout << "Enter book ID:"; cin >> bookID; book = findBook(bookArray, bookID); cardArray[card].booksOut++; bookArray[book].status = 1; return; break; case 2 : cout << "Enter card ID: "; cin >> cardID; card = findCard(cardArray, cardID); cout << "Enter book ID:"; cin >> bookID; book = findBook(bookArray, bookID); cardArray[card].booksOut--; bookArray[book].status = 0; return; break; case 3 : return; break; default : return; break; }}void library::showMenu(){ cout << "Welcome to the Library!" << endl << endl << "What do you want to do?" << endl << "B - Find a book" << endl << "C - Find a card" << endl << "T - Transaction" << endl << "O - Add/remove book" << endl << "A - Add/remove card" << endl << "Q - Quit" << endl;}void library::addRemBooks(book bookArray[]){ int command, bookID, i; cout << "Add or Remove book?" << endl << "1 - Add" << endl << "2 - Remove" << endl << "3 - Back" << endl; cin >> command; switch(command) { case 1 : cout << "Enter Book information:" << endl << "Book ID: "; cin >> bookArray[numBooks].bookID; cout << endl << "Title: "; cin >> bookArray[numBooks].title; cout << endl << "Author: "; cin >> bookArray[numBooks].author; cout << endl << "Card ID: "; cin >> bookArray[numBooks].cardID; cout << endl << "Status: "; cin >> bookArray[numBooks].status; numBooks++; return; break; case 2 : cout << "Enter book ID: "; cin >> bookID; i = findBook(bookArray, bookID); for(i; i < numBooks; i++) { bookArray = bookArray[++i]; } numBooks--; return; break; case 3 : return; break; }}void library::addRemCards(card cardArray[]){ int command, cardID, i; cout << "Add or Remove card?" << endl << "1 - Add" << endl << "2 - Remove" << endl << "3 - Back" << endl; cin >> command; switch(command) { case 1 : cout << "Enter card information:" << endl << "card ID: "; cin >> cardArray[numCards].cardID; cout << endl << "Name: "; cin >> cardArray[numCards].name; cout << endl << "Phone: "; cin >> cardArray[numCards].phone; cout << endl << "Books out: "; cin >> cardArray[numCards].booksOut; numCards++; return; break; case 2 : cout << "Enter card ID: "; cin >> cardID; i = findCard(cardArray, cardID); for(i; i < numCards; i++) { cardArray = cardArray[++i]; } numCards--; return; break; case 3 : return; break; }}#endif#ifndef BOOK_H#define BOOK_H#include <string>class book{ public: book(string, string, int, int, int); book(const book&); ~book(){} void print(book&); const book &operator=(const book&); int getNumber(); int getStatus(); int setStatus(int); int getHolder(); int setHolder(int); private: friend class library; string title, author; int status, bookID, cardID;};book::book(string title, string author, int bookID, int status, int cardID){ title = "\0"; author = "\0"; bookID = 0; status = 0; cardID = 0;}book::book(const book& bookToCopy){ title = bookToCopy.title; author = bookToCopy.author; bookID = bookToCopy.bookID; status = bookToCopy.status; cardID = bookToCopy.cardID;}void book::print(book&){ cout << "Name of book: " << title << endl << "Author of book: " << author << endl << "Book ID: " << bookID << endl << "Book Status: " << status << endl << "Book Holder ID: " << cardID << endl;}const book &book::operator=(const book& rhs){ if(&rhs != this) { title = rhs.title; author = rhs.author; bookID = rhs.bookID; status = rhs.status; cardID = rhs.cardID; } return *this;}int book::getNumber(){ return bookID;}int book::getStatus(){ return status;}int book::getHolder(){ return cardID;}int book::setStatus(int s){ status = (s >=-1 && s <= 1) ? s : 0;}int book::setHolder(int h){ cardID = (h > 0 && h < 1000000) ? h : 0;}#endif#include <iostream.h>#include <fstream.h>#include "library.h"int main() { library theLibrary(0, 0, NULL, NULL); char Command; bool do_exit = false; do { theLibrary.showMenu(); cin >> Command; do_exit = theLibrary.DoCommand( Command ); } while ( do_exit == false ); return 0;}
I'm surprised it even compiles given that
is illegal. You are only allowed to leave an array's leftmost bound unspecified if you are providing an initializer, which would let the compiler find out how many elements you have.
e.g. int foo[] = {0, 1, 2, 3};
book bookArray[]; card cardArray[];
is illegal. You are only allowed to leave an array's leftmost bound unspecified if you are providing an initializer, which would let the compiler find out how many elements you have.
e.g. int foo[] = {0, 1, 2, 3};
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement