Function to display array is displaying garbage as well

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * 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) )
	     return false;
	if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
	     return false;
	if ( (d==30) && (m==2) )
	     return false;
	if ( (m==2) && (d==29) && (y%4!=0) )
	     return false;
	if ( (m==2) && (d==29) && (y%400==0) )
	     return true;
	if ( (m==2) && (d==29) && (y%100==0) )
	     return false;
	if ( (m==2) && (d==29) && (y%4==0)  )
	     return true;

  return true;
}

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).
Last edited on
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.
Topic archived. No new replies allowed.