Loop going crazy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * book.cpp
 *
 *Simple program for storing array of book titles and published dates
 *
 *  Created on: Oct 9, 2010
 *      Author: Alpha
*/
#include <iostream>
#include <string>
using namespace 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 ()
{
    const int 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.
Topic archived. No new replies allowed.