Confuse about passing value back to class

Oct 18, 2010 at 4:46pm
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
/*
 * 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 dd, int mm, int yyyy);
	void printbook ();
};

void book::booktitles(string bookName)
{
	bookTitle = bookName;
}

void book::date(int dd, int mm, int yyyy)
{
	publishedDate = dd/mm/yyyy;
}

void book::printbook()
{
	cout << bookTitle << "published in " << publishedDate << endl;
}

int main ()

{
	book publishedTitle;
	string titles;
	cout << "Please enter the book title, press 99 to print previous records or press q to quit ";
	getline (cin, titles);
	publishedTitle.booktitles (titles);
	cout << "Please enter published date in DD/MM/YYYY format ";
	return 0;
}


I having some problem understanding the part on passing a value back to a private class. Had did a sample as above. Do highlight my mistake. Thanks for your time.
Oct 18, 2010 at 5:51pm
1
2
3
4
void book::date(int dd, int mm, int yyyy)
{
	publishedDate = dd/mm/yyyy;
}


/ divides two numbers.

dd / mm / yyyy takes 'dd' divides it by 'mm', then divides the result by 'yyyy' and stores the final result in published date.

Example:

dd=5
mm=2
yyyy=2010

dd/mm/yyyy -> 5 / 2 / 2010 -> (5 / 2) / 2010 -> 2 / 2010 -> 0

You'll probalby need to store the day/month/year all separately instead of storing them as a single 'publishedDate' member.

Either that or make a class to represent a full date.
Oct 19, 2010 at 3:33am
Hi Disch,

Thanks for pointing out this issue, will make the changes.

The area I had problem with is actually this portion.
I was expecting the cout to be "You have entered "booktitle" as your book title.
However, the output on screen was "You have entered as your book title ".

1
2
3
4
5
6
7
8
9
10
11
int main ()

{
	book publishedTitle;
	string titles;
	cout << "Please enter the book title, press 99 to print previous records or press q to quit ";
	getline (cin, titles);
	publishedTitle.booktitles (titles);
	cout << "Please enter published date in DD/MM/YYYY format ";
	return 0;
}


1
2
3
4
5
6
void book::booktitles(string bookName)
{
	book publishedTitle;
	bookTitle = bookName;
	cout << "You have entered " << publishedTitle.bookTitle << " as your book title " << endl;
}
Oct 19, 2010 at 2:49pm
Please enter the book title, press 99 to print previous records or press q to qu
it Foundation
Please enter published date in DD/MM/YYYY format
Process returned 0 (0x0)   execution time : 5.097 s
Press any key to continue.


// The above is what happened when I ran the program and entered a book title.(I ran the beginning post's code AND replaced it after with your newer code, both gave the same output and I didn't get a chance to enter a date)
Last edited on Oct 19, 2010 at 2:51pm
Oct 19, 2010 at 2:59pm
1
2
3
4
5
6
void book::booktitles(string bookName)
{
    book publishedTitle;
    bookTitle = bookName;
    cout << "You have entered " << publishedTitle.bookTitle << " as your book title " << endl;
}


Since booktitles is a member of your 'book' class, it can change and read 'bookTitle' directly without any other object. It's implied it is referring to this->bookTitle.

That is what is happening on line 4. You are correct assigning 'bookName' to this book's 'bookTitle'.

The problem is, you're creating another book (publishedTitle, created on line 3) and printing that other book's title (which you never assign) instead of this book's title. That's why nothing is being printed.

Basically what you're doing is similar to this:

1
2
3
4
5
6
7
void print5()
{
  int a;  // some other number
  int b = 5;  // the number we want to print

  cout << a; // printing the wrong number
}
Oct 19, 2010 at 3:07pm
// I'd like to thank Disch for posting that last code to simplify the problem. 'Tis helpful to me and I'm sure it's going to be somewhere in my brain if I ever come across a similar problem in the future!
Oct 20, 2010 at 2:48pm
Hi Disch, Sorry for the late reply.

This is the changes I had made.

1
2
3
4
5
6
7
void book::booktitles(string bookName)
{
	bookTitle = bookName;
	book publishedTitle;

	cout << "You have entered " << bookTitle << " as your book title" << endl;
}


1
2
3
4
5
6
7
8
9
10
11
int main ()

{
	book publishedTitle;
	string titles;
	cout << "Please enter the book title, press 99 to print previous records or press 00 to quit ";
	getline (cin, titles);
	publishedTitle.booktitles (titles);
	cout << "Please enter published date in DD/MM/YYYY format ";
	return 0;
}
Topic archived. No new replies allowed.