Okay, i tried going through class by class and cpp file by cpp file to catch errors but i'm still geting a few and i don't know what to do. the book.h file was given my the professor so it should be correct. Some questions: How do I set up the definition for the void Set funtion in the book.h file? How would I use the price function? Like, is it correct how I have it. Can someone just take a look at my code please and try to tell me what I'm doing wrong without doing the work for me?
The problem statement is in the previous post.
Here's my code with code tags (hopefully they work this time)
book.h file given by professor
Code:
#ifndef _BOOK_H
#define _BOOK_H
#include <iostream>
using namespace std;
enum Genre {FICTION, MYSTERY, SCIFI, COMPUTER};
class Book
{
public:
Book(); // default constructor, sets up blank book object
#ifndef _BOOK_H
#define _BOOK_H
#include <iostream>
using namespace std;
enum Genre {FICTION, MYSTERY, SCIFI, COMPUTER};
class Book
{
public:
Book(); // default constructor, sets up blank book object
void Set(char* t, char* a, Genre g, double p);
// the Set function should allow incoming data to be received through
// parameters and loaded into the member data of the object. (i.e.
// this function "sets" the state of the object to the data passed in).
// The parameters t, a, g, and p represent the title, author, genre,
// and price of the book, respectively.
const char* GetTitle(); // returns the title stored in the object
const char* GetAuthor(); // returns the author
double GetPrice(); // returns the price
Genre GetGenre(); // returns the genre
void Display(); // described below
private:
char title[31]; // may assume title is 30 characters or less
char author[21]; // may assume author name is 20 characters or less
Genre type;
double price;
};
/* Display() function
The member function Display() should print out a Book object on one line
as follows, in an organized manner. (Monetary output should be in dollars
and cents notation, to two decimal places):
Title Author Genre Price
Examples:
Programming for Dummies Marvin Dipwart Computer $ 24.95
Mutant Space Weasels Bob Myers Sci-Fi $ 5.95
*/
#endif store.h file
Code:
#ifndef _STORE_H
#define _STORE_H
//#include "book.h"
#include "menu.cpp"
class Store
{
public:
Store();
~Store();
void Add();
void Find();
void Sell();
void Display();
void Genre();
void ShowMenu();
void Exit();
private:
int maxSize,
currentSize;
Entry* entryList;
void Grow();
int FindBook(char* aBook);
};
#endif
menu.cpp file
Code:
#include <cctype> // for toupper
#include <iostream> // for cin, cout
#include "store.h" // for class Directory
using namespace std;
void DisplayMenu()
// Display the main program menu.
{
cout << "\n\t\t*** Bookstore Menu ***";
cout << "\n\tA \tAdd book to inventory";
cout << "\n\tF \tFind a book form inventory";
cout << "\n\tS \tSell a book";
cout << "\n\tD \tDisplay the inventory list";
cout << "\n\tG \tGenre summary";
cout << "\n\tM\tShow this Menu";
cout << "\n\tX \tExit the Program";
}
char GetAChar(char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response; // the char to be returned
cout << promptString; // Prompt the user
cin >> response; // Get a char,
response = toupper(response); // and convert it to uppercase
cin.get(); // Discard newline char from input.
return response;
}
char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return ((c == 'A') || (c == 'F') || (c == 'S') || (c == 'D') ||
(c == 'G') || (c == 'M') || (c == 'X'));
}
char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar("\n\n>"); // Get a command character.
while (!Legal(cmd)) // As long as it's not a legal command,
{ // display menu and try again.
cout << "\nIllegal command, please try again . . .";
ShowMenu();
cmd = GetAChar("\n\n>");
}
return cmd;
}
void main()
{
Menu m; // Create and initialize a new directory.
ShowMenu(); // Display the menu.
char command; // menu command entered by user
do
{
command = GetCommand(); // Retrieve a command.
switch (command)
{
case 'A': m.Add(); break;
case 'F': m.Find(); break;
case 'S': m.Sell(); break;
case 'D': m.Display(); break;
case 'G': m.genre(); break;
case 'M': ShowMenu(); break;
case 'X': break;
}
} while (command != 'X');
} Store.cpp
Code:
#include <iostream>
#include <cstring>
//#include "book.h"
#include "store.h"
using namespace std;
Store::Store()
{
maxSize = 20;
currentSize = 0;
entryList = new Entry[maxSize];
}
Store::~Store()
// This destructor function for class Store
// deallocates the menu's list of Entry objects.
{
delete [] entryList;
}
void Store::Add()
// Insert a new entry into the store.
{
if (currentSize == maxSize) // If the directory is full, grow it.
Grow();
entryList[currentSize++].Load(); // read new entry.
}
void Store::Find()
{
char aBook[20];
cout << "\tType the title of the book to be looked up, followed by <Enter>: ";
cin.getline(aBook, 31);
int thisEntry = FindBook(aBook);
if (thisEntry == -1)
cout << aBook << " not found in current menu\n";
else
{
cout << "\nEntry found: ";
entryList[thisEntry].Show(); // display entry.
}
}
void Store::Sell()
{
char aBook[31];
cout << "\nType title to be removed, followed by <Enter>: ";
cin.getline(aBook, 31);
int thisEntry = FindBook(aBook);
if (thisEntry == -1)
cout << aBook << " not found in directory";
else
{
// Shift each succeding element "down" one position in the
// Entry array, thereby deleting the desired entry.
for (int j = thisEntry + 1; j < currentSize; j++)
entryList[j - 1] = entryList[j];
currentSize--; // Decrement the current number of entries.
cout << "Entry removed.\n";
}
}
void Store::Display()
{
if (currentSize == 0)
{
cout << "\nStore is currently empty.\n";
return;
}
// Display a header.
cout << "\n\t***Title***\t\t***Author***\t\t***Genre***\t\t***Price***\n\n";
for (int i = 0; i < currentSize; i++) // For each entry,
entryList[i].Show(); // send it to output
}
void Store::Grow()
// Double the size of the store's entry list
// by creating a new, larger array of entries
// and changing the store's pointer to refer to
// this new array.
{
maxSize = currentSize + 5; // Determine a new size.
Entry* newList = new Entry[maxSize]; // Allocate a new array.
for (int i = 0; i < currentSize; i++) // Copy each entry into
newList[i] = entryList[i]; // the new array.
delete [] entryList; // Remove the old array
entryList = newList; // Point old name to new array.
}
int Store::FindBook(char* aBook)
// Locate a book in the store. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
for (int i = 0; i < currentSize; i++) // Look at each entry.
if (strcmp(entryList[i].GetTitle(), aBook) == 0)
return i; // If found, return position and exit.
return -1; // Return -1 if never found.
} Book.cpp file
Code:
#include <iostream>
#include <cstring> // for strlen, strcpy
//#include "book.h" // for class Entry
using namespace std;
Book::Book()
// This constructor for class Entry initializes the name, phone number,
// and room number to be blank strings.
{
strcpy(title, " ");
strcpy(author, " ");
strcpy(type, " ");
//strcpy(price, " ");
}
void Book::Set( char* t, char* a, Genre g, double p )
// allows us to read in the data from the keyboard
{
cout << "\nType title, followed by RETURN or ENTER: ";
cin.getline(t, 31);
cout << "\nType name of author, followed by RETURN or ENTER: ";
cin.getline(a, 21);
cout << "\nType price of book, followed by RETURN or ENTER: ";
cin.getline(p);
}
char*Book::GetTitle() const
{
return title;
}
char*Book::GetAuthor() const
{
return author;
}
double Book::GetPrice()
{
return price;
}
enum Book::GetGenre()
{
return type;
}
void Book::Display()
{
int i;
cout << '\t' << Title; // Display name (after tabbing).
// Display remaining blanks, so that data lines up on screen.
for (i = strlen(title) + 1; i < 31; i++)
cout.put(' ');
cout << '\t' << Author; // Display phone number.
for (i = strlen(author) + 1; i < 20; i++)
cout.put(' ');
cout << '\t' << Genre;
cout << '\n';
cout<<'\t'<<Price;
cout<<'\n';
}