problems with getline function

Hi,
I'm new at C++ programming so I was writing a practice program. The idea of the program is to create some sort of listing in txt file of the books you have. So, my idea is to create an array of book structures (where you can store the title, author and year) and then write this array to a txt. The problem is that when I ask to enter the title and the author of the book (in the function getbook, lines 41-50) with the getline function it automatically stores a blank string to the first question and only lets introduce you the answer to the second question. I'm sure it's kind of a silly mistake, I would appreciate the help. Thank you very much.
Here is the code,

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct book{
	string title;
	string author;
	int year;
	bool empty;
};

void getbook(int n);

void ini();

void writedatabase();

void showdatabase();

#define SIZE 200
book database[SIZE];

int main () 
{
	
	int num;
	ini();
	cout << "How many books do you want to enter? (less than " << SIZE <<" )\n"; 
	cin >> num;
	for (int n=0;n<=num-1;n++)
	{
		getbook(n);
	}
	writedatabase();
	showdatabase();
	
	return 0;
}

void getbook(int n)
{
	cout << "Title: ";
	getline ( cin, database[n].title);
	cout << "Author: ";
	getline ( cin, database[n].author);
	cout << "Year:";
	cin >>	database[n].year;
	database[n].empty=0;
}

void ini()
{
	for (int j=0;j!=SIZE-1;j++)
	{
		database[j].title="";
		database[j].author="";
		database[j].empty=1;
	}
}

void writedatabase()
{

}

void showdatabase()
{
	for (int j=0;j!=SIZE-1;j++)
	{
		if (database[j].empty==0)
		{
			cout << database[j].title << endl;
			cout << database[j].author << endl;
			cout << database[j].year << endl;
		}
	}
}
cin >> num;

This line will leave an extra \n in the buffer, which means when you get to your next getline(), it will read the \n and give you an empty string.

Read: http://www.cplusplus.com/forum/beginner/15260/page2.html#msg75474
Ok, thak you very much. I've corrected it.
By the way, is there a way to clear the buffer?
Topic archived. No new replies allowed.