You are here:

C++/How does it display ???


I have a problem in my homework ! I don't know how it display on screen .Can you help me please !.
My code :

#include <iostream>
#include <iomanip>
using namespace std;
ostream& OutputDate (ostream& out, int& m, int& d, int& y);
int main () {
int month, day, year;
char c;
cout << OutputDate (cout, month, day, year) << " ";
ostream& OutputDate (ostream& out, int& m, int& d, int& y){
out << setw (2) << setfill ('0') << m << '-'
<< setw (2) << d << '-' << setw (4) << y
<< setfill (' ');
return out;

Input : 1-2-2001
Out put : 01-02-20010047B7F4
0047B7F4--> I don't know where ?? .
I'm sorry because My English is very bad .Thanks for all .  

First of all I would appreciate it if you could be bothered to post code that is laid out in a reasonable way. It took me quite a lot of reformatting to see what your code actually does. Mostly you need to ensure that you have line breaks in a few places and that the code is properly indented for example:

ostream& OutputDate (ostream& out, int& m, int& d, int& y); int main () { int month, day, year; char c;


ostream& OutputDate (ostream& out, int& m, int& d, int& y);

int main ()
   int month, day, year; char c;

The original of your code may have looked reasonable and maybe in the copy, paste and posting through AllExperts operations it lost something. I hope so because if you ever presented me with such code professionally I would not hire you or fire you and if as course work I would fail you.

Remember you are asking someone else (me) to look at your code to help you with a problem. It would be polite if you bothered to make it look presentable. Further it is in your interest to do so: if I cannot understand your code because I cannot see what it is doing because it is such a mess then you are not likely to get such a good (or any) answer.

Now the problem is this line:

cout << OutputDate (cout, month, day, year) << " ";

(again with the extra 'lines' removed from the end).

Why are you passing the return value of the function OutputDate to std::cout::operator<< ?

If you think about it you can expand the above to the following two lines:

ostream & returnedStream = OutputDate (cout, month, day, year);
cout << returnedStream << " ";

All I have done is saved the returned value from the call to OutputDate in an appropriate variable and passed this to std::cout. This is a similar transform to the following:

cout << tan(theta) << " ";

Can be replaced by:

double tan_theta = tan( theta );
cout << tan_theta << " ";

With this change the program will behave just as the original code does. It outputs the date in the call to OutputDate and then outputs the address of the stream object std::cout and a space.

Now I think you can see where the odd number comes from. It is the address of the stream object returned from OutputDate, which it appears is the result of passing such an object (or reference to object) to the std::cout::operator<< function - i.e. cout << .

Considering that you go to the trouble of returning the passed in stream reference from OutputDate why do you need to mention cout elsewhere on the

cout << OutputDate (cout, month, day, year) << " ";

line. Consider what the following means:

OutputDate(cout, month, day, year) << " ";

Finally, why are you passing the day, month and year to OutputDate by reference rather than by value? They are not big items, on a typical 32-bit system an int and a reference are both 32 bits (or 4 bytes) in size. Neither do they need to be output parameters (output or out parameters act like additional return values). As this is a function for outputting values they certainly do not need to be changed. So if passing them by reference at least pass by constant reference:

ostream& OutputDate
(ostream& out
, int const & m
, int const & d
, int const & y
// ...

(I spread the parameter list over several lines as it wrapped in my word processor.)

Better yet just pass them by value:

ostream& OutputDate( ostream & out, int m, int d, int y )
// ...

Oh, some prompts on what I was to input would have been useful - I enter dates in dd-mm-yyyy format as I live in the UK.

Hope this is of help.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.