I changed my code (see below) and there are still problems. Namely, It completely skips over the input for the author and the performer. For example, say I put in B for book then the next output is:
Enter book title: Enter book author:
I can then input data for the author but I can't put in anything for book title. Why is it doing that. My complete code is below. Thank You!
Code:
//holding.h
#ifndef _Holding_H
#define _Holding_H
#include <iostream>
class Holding
{
public:
Holding();
Holding(const char *str);
virtual void settitle() =0;
virtual void setcallnum() =0;
virtual void print() =0;
virtual ~Holding();
private:
char *buf;
};
#endif
//holding.cpp
#define _CRT_SECURE_NO_DEPRECATE 1
#include <iostream>
#include "holding.h"
using namespace std;
Holding::Holding()
{
buf = new char[1];
buf[0] = '\0';
}
Holding::Holding(const char *str)
{
buf = new char[strlen(str) + 1];
strcpy(buf, str);
}
Holding::~Holding()
{
delete []buf;
}
//book.h
#ifndef _Book_H
#define _Book_H
#include <iostream>
#include <string>
#include "holding.h"
class Book: public Holding
{
public:
Book();
Book(const char *s);
virtual ~Book();
virtual void settitle();
void setbookauthor();
virtual void setcallnum();
virtual void print();
private:
std::string booktitle;
std::string bookauthor;
std::string bookcallnum;
char *bbuf;
};
#endif
//book.cpp
#define _CRT_SECURE_NO_DEPRECATE 1
#include <iostream>
#include <string>
#include "book.h"
using namespace std;
Book::Book()
{
bbuf = new char[1];
bbuf[0] = '\0';
}
Book::Book(const char *s)
{
bbuf = new char[strlen(s) + 1];
strcpy(bbuf, s);
}
Book::~Book()
{
delete []bbuf;
}
void Book::settitle()
{
cout<< "Enter book title: ";
std::getline(cin, booktitle);
}
void Book::setbookauthor()
{
cout<< "Enter book author: ";
std::getline(cin, bookauthor);
}
void Book::setcallnum()
{
cout<< "Enter call number: ";
std::getline(cin, bookcallnum);
}
void Book::print()
{
cout<< "BOOK: "<< booktitle << " " << bookauthor << " " << bookcallnum << " " << endl;
}
//record.h
#ifndef _Recording_H
#define _Recording_H
#include <iostream>
#include <string>
#include "holding.h"
class Recording: public Holding
{
public:
Recording();
Recording(const char *str);
virtual ~Recording();
virtual void settitle();
void setrecperformer();
virtual void setcallnum();
void setrecformat();
virtual void print();
private:
std::string rectitle;
std::string recauthor;
std::string reccallnum;
std::string recformat;
char *rbuf;
};
#endif
//record.cpp
#define _CRT_SECURE_NO_DEPRECATE 1
#include <iostream>
#include "record.h"
using namespace std;
Recording::Recording()
{
rbuf = new char[1];
rbuf[0] = '\0';
}
Recording::Recording(const char *str)
{
rbuf = new char[strlen(str) + 1];
strcpy(rbuf, str);
}
Recording::~Recording()
{
delete []rbuf;
}
void Recording::settitle()
{
cout<< "Enter recording title: ";
std::getline(cin, rectitle);
}
void Recording::setrecperformer()
{
cout<< "Enter performer: ";
std::getline(cin, recauthor);
}
void Recording::setcallnum()
{
cout<< "Enter call number: ";
std::getline(cin, reccallnum);
}
void Recording::setrecformat()
{
cout<< "Enter format: (L)P, (C)assette, (R)eel_to_reel, (D)isk:" ;
std::getline(cin, recformat);
}
void Recording::print()
{
cout<< "RECORDING: "<< rectitle << recauthor << recformat << reccallnum << endl;
}
//library.h
#include <iostream>
#include "holding.h"
#include "book.h"
#include "record.h"
//library.cpp
#include <iostream>
#include "library.h"
using namespace std;
int main()
{
Holding *ptr[5];
Book *bptr;
Recording *rptr;
char choice;
cout<< "Enter holdings to be stored in a list: " << endl;
for(int i = 0; i<5; i++)
{
cout<< "Enter B for book, R for recording: ";
cin>> choice;
if(choice == 'b' || choice == 'B')
{
bptr = new Book;
bptr->settitle();
bptr->setbookauthor();
bptr->setcallnum();
ptr[i] = bptr;
}
if(choice == 'r' || choice == 'R')
{
rptr = new Recording;
rptr->settitle();
rptr->setrecperformer();
rptr->setrecformat();
rptr->setcallnum();
ptr[i] = rptr;
}
}
for(int k = 0; k<5; k++)
ptr[k]->print();
}