/*
* book.cpp
*
*Simple program for storing array of book titles and published dates
*
* Created on: Oct 9, 2010
* Author: Alpha
*/
#include "books.h"
//implementation section
void Book::setTitle(string bookName)
{
bookTitle = bookName;
}
string Book::getTitle()
{
return bookTitle;
}
void Book::setYear (int publishedYear)
{
year = publishedYear;
}
int Book::getYear ()
{
return year;
}
void Book::setMonth (int publishedMonth)
{
month = publishedMonth;
}
int Book::getMonth ()
{
return month;
}
void Book::setDay (int publishedDay)
{
day = publishedDay;
}
int Book::getDay ()
{
return day;
}
bool checkDate(int m, int d, int y) //checking of day & leap year
{
if (! (1<= d && d<=31) )
returnfalse;
if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
returnfalse;
if ( (d==30) && (m==2) )
returnfalse;
if ( (m==2) && (d==29) && (y%4!=0) )
returnfalse;
if ( (m==2) && (d==29) && (y%400==0) )
returntrue;
if ( (m==2) && (d==29) && (y%100==0) )
returnfalse;
if ( (m==2) && (d==29) && (y%4==0) )
returntrue;
returntrue;
}
void Book::printBooks()
{
cout << bookTitle << " - " << "[" << day << "/" << month << "/" << year << "]" << endl;
}
int main ()
{
string title;
int x, yr, mm, dd;
Book books[SIZE];
Book date[SIZE];
for (x = 0; x<SIZE; ++x)
{
//get the book title
cout << "Enter book title or type exit to quit" << endl;
getline (cin, title);
if (title == "exit")
{
//cout << "Bye Bye";
break;
}
books[x].setTitle(title);
//get the year
cout << "Enter the published date." << endl;
cout << "Enter the year: ";
cin >> yr;
while (yr < 1582 || yr > 2010) //gregorian calendar started in 1582
{
cout << "Please enter a range between 1582 - 2010" << endl
<< "Reenter the year: " << endl;
cin >> yr;
}
date[x].setYear(yr);
cin.ignore();
//get the month
cout << "Enter the month: ";
cin >> mm;
while (mm < 1 || mm > 12)
{
cout << "Please enter a range between 1 - 12" << endl
<< "Reenter the month: " << endl;
cin >> mm;
}
date[x].setMonth(mm);
cin.ignore();
//get the day
cout << "Enter the day: ";
cin >> dd;
//use bool checkDate function
while (!checkDate(mm, dd, yr))
{
cout << "Invalid day" << endl
<< "Reenter the day: " << endl;
cin >> dd;
}
date[x].setDay(dd);
cin.ignore();
//print out what user has just entered
cout << "Book added: " << title << " - " << "[" << dd
<< "/" << mm << "/" << yr << "]" << endl;
}
for (x = 0; x < SIZE; x++)
{
books[x].printBooks();
date[x].printBooks();
}
return 0;
}
Enter book title or type exit to quit
harry potter
Enter the published date.
Enter the year: 1985
Enter the month: 11
Enter the day: 06
Book added: harry potter - [6/11/1985]
winnie the pooh - [0/-917859203/1986530702]
- [14/11/1985]
dark knight - [2293456/4258160/0]
- [25/11/1985]
harry potter - [-1090492655/1986585725/2293700]
- [6/11/1985]
As can be seen from the output, my printBooks() is displaying some garbage data as well (bold).
You are doing something very strange.
You make two arrays:
1 2 3
Book books[SIZE];
Book date[SIZE];
You then proceed to put the title of the book in the books array, and the date for that book in the date array.
This means that for each book entry in the books array you have a good title but rubbish dates because no date is set in this array
and in the date array you have no title (because the default constructor for a string is an empty string) and good values for the date for that book
You then do a print out:
1 2 3 4 5 6
for (x = 0; x < SIZE; x++)
{
books[x].printBooks();
date[x].printBooks();
}
So you get alternating lines of good title with a rubbish date - followed by no title with a good date.