# 2 Re: compiles but wont run
//////////////////////Main Program///////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstring>
#include "ISBN.h"
#include "Book.h"
using namespace std;
const int NUMBER_OF_BOOKS = 15;
void check_isbn_class();
void check_book_class();
int main() {
check_isbn_class();
check_book_class();
return 0;
}
void check_isbn_class() {
char isbns[13][14];
int index=0;
strcpy(isbns[index++], "1-57676-074-X");
strcpy(isbns[index++], "0-12-597121-2"); // correct check digit is 4
strcpy(isbns[index++], "0-385-49532-3");
strcpy(isbns[index++], "0-321-19724-X"); // correct check digit is 0
strcpy(isbns[index++], "0-02-360692-4");
strcpy(isbns[index++], "0-321-19362-5"); // correct check digit is 8
strcpy(isbns[index++], "0-201-70433-1");
strcpy(isbns[index++], "1-56592-324-2"); // correct check digit is 3
strcpy(isbns[index++], "0-321-19498-5");
strcpy(isbns[index++], "0-13-034074-9"); // correct check digit is X
strcpy(isbns[index++], "0-13-790395-2");
strcpy(isbns[index++], "0-471-46983-0"); // correct check digit is 1
strcpy(isbns[index++], "0-670-82162-4");
for (int i=0; i<13; i++) {
ISBN isbn(isbns[i]);
isbn.print(cout);
if (isbn.valid())
cout << " is valid\n";
else cout << " is not valid\n";
}
ISBN isbn1("0", "670", "82162"),
isbn2("0", "13", "034074");
isbn1.print(cout); // should print "0-670-82162-4"
cout << endl;
isbn2.print(cout); // should print "0-13-034074-X"
cout << endl;
}
void check_book_class() {
Book texts[NUMBER_OF_BOOKS];
char title[81], author[81], isbn[14];
char publisher[8];
ifstream infile;
infile.open( "books.txt" );
if (!infile) {
cout << "Unable to open file books.txt\n";
return;
}
for (int i=0; i<NUMBER_OF_BOOKS; i++) {
infile.getline(title, 81);
cout << title << endl;
infile.getline(author, 81);
infile.getline(isbn, 14);
Book thisbook(title, author, isbn);
cout << "just read this book:\n";
thisbook.print(cout);
cout << endl;
texts[i] = thisbook;
}
cout << "Here are all of the books in the database\n\n";
for (int i=0; i<NUMBER_OF_BOOKS; i++) {
texts[i].print(cout);
cout << endl;
}
while (true) {
cout << "Enter a publisher code, or 'quit' to stop\n";
cin >> publisher;
if (strcmp(publisher, "quit") == 0) return;
cout << "Books by this publisher:\n\n";
for (int i=0; i<NUMBER_OF_BOOKS; i++)
if (strcmp(texts[i].getpublisher(), publisher) == 0) {
texts[i].print(cout);
cout << endl;
}
}
}
////////////////ISBN Class/////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
class ISBN
{
public:
ISBN();
ISBN(char[ ]); // e.g., ISBN book("0-670-82162-4");
ISBN(char[ ], char[ ], char[ ], char); // 4-argument constructor is passed
// group code, publisher code, book
// code, and check character. e.g.
// ISBN book("0","670","82162","4");
ISBN(char[ ], char[ ], char[ ]); // 3-argument constructor is passed
// group code, publisher code, and
// book code. It computes the
// check character
// e.g. ISBN book("0","670","82162");
bool valid(); // true if check character is correct
char *getpublisher(); // return publisher code
void print(ostream &o); // print string with hyphens
int char_to_int(char c);
private:
char group[6]; // group code is 1-5 digits
char publisher[8]; // publisher code is 1-7 digits
char book[8]; // book code is 1-7 digits
char check;
int concat2[20];
char compute_check(); // compute the check character
};
ISBN::ISBN(char a[], char b[], char c[], char d)
{
strcpy(group, a);
strcpy(publisher, b);
strcpy(book, c);
check = d;
}
ISBN::ISBN(char a[], char b[], char c[])
{
strcpy(group, a);
strcpy(publisher, b);
strcpy(book, c);
check = compute_check();
}
ISBN::ISBN(char a[])
{
char deliminator[] = "-";
char check2[2];
strtok(a, deliminator);
strcpy(group, a);
strtok(NULL, deliminator);
strcpy(publisher, NULL);
strtok(NULL, deliminator);
strcpy(book, NULL);
strtok(NULL, deliminator);
strcpy(check2, NULL);
check = check2[0];
}
ISBN::ISBN(){}
char ISBN::compute_check()
{
char concat1[20];
strcat(concat1, group);
strcat(concat1, publisher);
strcat(concat1, book);
for (int i = 0; i <= 9; i++)
{
concat2[i] = char_to_int(concat1[i]);
}
int checkNumber = (concat2[0] * 1) + (concat2[1] * 2) + (concat2[2] * 3) + (concat2[3] * 4) + (concat2[4] * 5) + (concat2[5] * 6) + (concat2[6] * 7) + (concat2[7] * 8) + (concat2[8] * 9);
checkNumber = checkNumber % 11;
char checkChar = checkNumber;
if (checkNumber = 10)
checkChar = 'X';
return checkChar;
}
bool ISBN::valid()
{
char checkTest = compute_check();
return (checkTest = check);
}
char * ISBN::getpublisher()
{
return publisher;
}
void ISBN::print(ostream &o)
{
cout<<group<<"-"<<publisher<<"-"<<book<<"-"<<check<<endl;
}
//Function used to convert chars to int.
int ISBN::char_to_int(char c)
{
return c - '0';
}
///////////////////////BOOK CLASS///////////////////////////////////
#include <iostream>
#include "ISBN.h"
using namespace std;
class Book {
private:
char title[81]; // title no more than 80 characters
char author[51]; // author no more than 50 characters
ISBN isbn; // the book's ISBN
public:
Book();
Book(char[ ], char[ ], char[ ]); // e.g., ISBN book("The Odyssey",
// "Homer", "0-670-82162-4");
char *gettitle();
char *getauthor();
ISBN getisbn();
char *getpublisher(); // look up the publisher code
// in the isbn
void print(ostream &o); // print title, author, and isbn
};
Book::Book(char a[ ], char b[ ], char c[ ])
{
strcpy(title, a);
strcpy(author, b);
ISBN isbn(c);
}
Book::Book(){}
char * Book::gettitle()
{return title;}
char * Book::getauthor()
{return author;}
ISBN Book::getisbn()
{return isbn;}
char * Book::getpublisher()
{
return isbn.getpublisher();
}
void Book::print (ostream &o)
{
cout<<title<<", "<<author<<", "<<endl;
isbn.print(cout);
}