delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/08/21:01:39

From: "Chris Hut" <chut AT wesleyan DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: extreeeeme newbie question
Date: Thu, 8 Apr 1999 20:28:55 -0400
Organization: Wesleyan University
Lines: 41
Message-ID: <7ejhhq$iqd@news.wesleyan.edu>
References: <7ehdb0$3o2$1 AT fir DOT prod DOT itd DOT earthlink DOT net>
NNTP-Posting-Host: chut.stu.wesleyan.edu
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Jason Nehf <zolaar AT earthlink DOT net> wrote in message
news:7ehdb0$3o2$1 AT fir DOT prod DOT itd DOT earthlink DOT net...
> Okay, I'm trying to write a simple "Hello, world!" program.  this is the
> code:
>
> #include <iostream.h>
> #include <conio.h>
>
> int main()
> {
>     clrscr();
>     cout << "Hello, world!";
>     getch();
> }
>
> But for some reason, the getch() is called first, THEN the cout, and the
> clrscr() is never called at all!!

I think this has something to do w/ the way output streams are handled in
C++.  "Hello, world!" is sent to the output buffer, but not displayed until
the buffer is "flushed."  The buffer isn't flushed to the screen until
either a newline character ("\n") is read or input is pending.  Adding \n to
the end of your string makes the program work properly, so does adding " <<
flush;" to the end of the cout line; my guess is that C++ doesn't know that
getch() means that input is pending, and just waits til the end of main() is
reached, AFTER the getch() function.  AFAIK isn't getch() a nonstandard C
library function?  using the istream buffer (a cin function) would probably
work better.  I *think* generally you want to try to avoid including
non-standard C libraries, b/c they don't mesh well w/ some C++ functions
(such as in this case).

Sorry if this is confusing, I'm not quite sure I have it all down myself
:-/, esp in terms of what's acceptable to include as far as C libraries go
(I know that cstdlib, cstring, ctime and cctype are quite frequently used,
so I'm wondering where that particular line is drawn...?).  But hope this
helps.

Chris


- Raw text -


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