/*
* book.cpp
*
*Simple program for storing array of book titles and published dates
*
* Created on: Oct 9, 2010
* Author: Alpha
*/
#include <iostream>
#include <string>
usingnamespace std;
//declaration section
class book
{
private:
string bookTitle;
int publishedDate;
public:
void booktitles (string);
void date (int);
void printbook ();
};
// For displaying book name
void book::booktitles(string bookName)
{
bookTitle = bookName;
cout << "You have entered " << bookTitle << " as your book title" << endl;
}
// For displaying date
void book::date(int date)
{
publishedDate = date;
//cout << "You have entered " << publishedDate << " as your book published date" << endl;
}
//Output all data in array
void book::printbook()
{
}
int main ()
{
constint SIZE = 10;
string titles;
int x;
int dd;
int mm;
int yy;
book publishedTitle[SIZE];
book publishedDate[SIZE];
for (x = 0; x < SIZE; ++x)
{
cout << "Please enter the book title, or type exit to quit ";
getline (cin, titles);
if (titles == "exit")
{
cout << "Bye Bye";
return 0;
}
publishedTitle[x].booktitles (titles);
cout << "Enter the month book was published in MM format ";
cin >> mm;
cin.ignore();
publishedDate[x].date (mm);
cout << "Enter the day book was published in DD format ";
cin >> dd;
cin.ignore();
publishedDate[x].date (dd);
cout << "Enter the year book was published in YY format ";
cin >> yy;
cin.ignore();
publishedDate[x].date (yy);
}
return 0;
}
Need some advice on how to do a date validation for such situation.
Firstly you make 2 lists of books here. One will have the dates and one the titles. there is no reason to to this since a book in your case has a date and title. You need to make just one list of books and set there data and title.
Then, you store the month in the book object, after this you override it with the day and then override it with the year. In the end you book will only hold the year and nothing else.
Your best option is to redefine you book class to something like this:
1 2 3 4 5 6 7 8 9 10 11 12
class book
{
private:
string bookTitle;
int publishedDateM;
int publishedDateD;
int publishedDateY;
public:
void booktitles (string);
void date (int M, int D, int Y);
void printbook ();
};
or even better is making a struct that holds a date like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct Date
{
int Month;
int Day;
int Year;
}
class book
{
private:
string bookTitle;
Date publishedDate;
public:
void booktitles (string);
void date (Date);
void printbook ();
};
#include <iostream>
#include <string>
usingnamespace std;
//declaration section
struct Date
{
int Month;
int Day;
int Year;
};
class book
{
private:
string bookTitle;
Date publishedDate;
public:
void booktitles (string);
void date (Date);
void printbook ();
};
// For displaying book name
void book::booktitles(string bookName)
{
bookTitle = bookName;
cout << "You have entered " << bookTitle << " as your book title" << endl;
}
// For displaying date
void book::date(Date date)
{
publishedDate = date;
//cout << "You have entered " << publishedDate << " as your book published date" << endl;
}
//Output all data in array
void book::printbook()
{
}
int main ()
{
constint SIZE = 10;
string titles;
int x;
int dd;
int mm;
int yy;
book publishedTitle[SIZE];
book publishedDate[SIZE];
for (x = 0; x < SIZE; ++x)
{
cout << "Please enter the book title, or type exit to quit ";
getline (cin, titles);
if (titles == "exit")
{
cout << "Bye Bye";
return 0;
}
Date newBookDate;
publishedTitle[x].booktitles (titles);
cout << "Enter the month book was published in MM format ";
cin >> newBookDate.Month;
cin.ignore();
cout << "Enter the day book was published in DD format ";
cin >> newBookDate.Day;
cin.ignore();
cout << "Enter the year book was published in YY format ";
cin >> newBookDate.Year;
cin.ignore();
publishedDate[x].date (newBookDate);
}
return 0;
}