/*
* 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 ();
};
void book::booktitles(string bookName)
{
bookTitle = bookName;
cout << "You have entered " << bookTitle << " as your book title" << endl;
}
void book::date(int date)
{
publishedDate = date;
cout << "You have entered " << publishedDate << " as your book published date" << endl;
}
void book::printbook()
{
}
int main ()
{
constint SIZE = 10;
string titles;
int x;
int dd;
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;
}
else
publishedTitle[x].booktitles (titles);
cout << "Enter day book was published in DD format ";
cin >> dd;
publishedDate[x].date (dd);
}
return 0;
}
Hi attached is a code that allow users to input 10 book title & it's published date. It is still a work in progress thus pardon me for the mess.
Please enter the book title, or type exit to quit winnie the pooh
You have entered winnie the pooh as your book title
Enter day book was published in DD format 14
You have entered 14 as your book published date
Please enter the book title, or type exit to quit You have entered as your book title
Enter day book was published in DD format
As can be seen from the output, my program fail after the bold line.
Hope someone can advice. Thanks in advice.
'cin >>' leaves a newline char in the input stream. This '\n' is then read by getline(), so you don't get to enter anything. Remove it with cin.ignore() after line 61, for example.