delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/03/15:47:05

From: Chong Kai Xiong <w_velocity AT yahoo DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Strange output formatting!
Date: Mon, 03 May 1999 19:19:46 +0800
Organization: Singapore Telecommunications Ltd
Lines: 28
Message-ID: <372D8652.5D542B5C@yahoo.com>
References: <xq5XfAAYjEL3EwZi AT quayles1 DOT demon DOT co DOT uk>
NNTP-Posting-Host: as002808.singnet.com.sg
Mime-Version: 1.0
X-Mailer: Mozilla 4.04 [en] (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Actually, there's nothing wrong with the output, at least it did what you
specified. What you got has to do with the way output streams (ostreams)
work...when you write

    cout << "Some box data is " << get_volume(x, y, z) << "\n";

it actually translates to:

    int Temp = get_volume(x, y, z);
    cout << "Some box data is ";
    cout << Temp << "\n";

Can't really phrase the explanation but here goes my bad English =) :
what really happens is that the string "Some box data" is first fed into
a output stream. Next the program calls get_volume() to obtain the result
value to output which is then appended onto the stream. Next, "\n" is
added. The data stream is then directed to 'cout' which starts printing
it character by character  to screen from the current cursor position.
printf() outputs directly to screen and does not affect the stream, which
is why you didn't get the dimensions printed in the correct position.
The  function was called first before the stream was outputted to screen,
so the dimensions appeared in front instead. See my point? I hope my
lengthy explanation helped... =)

Have a nice day...

Descender

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019