delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/15/18:35:29

From: "Edmund Horner" <ejrh AT paradise DOT net DOT nz>
Newsgroups: comp.os.msdos.djgpp
References: <tX425.32454$hp4 DOT 768849 AT newsread1 DOT prod DOT itd DOT earthlink DOT net>
Subject: Re: Newbie DJGPP compiler question
Lines: 133
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4029.2901
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4029.2901
Organization: Paradise Net Ltd. Customer
Message-ID: <961077643.986871@shelley.paradise.net.nz>
Cache-Post-Path: shelley.paradise.net.nz!unknown AT 203-79-93-29 DOT tnt11 DOT paradise DOT net DOT nz
X-Cache: nntpcache 2.4.0b2 (see http://www.nntpcache.org/)
Date: Fri, 16 Jun 2000 02:07:41 +1200
NNTP-Posting-Host: 203.96.152.26
X-Complaints-To: newsadmin AT xtra DOT co DOT nz
X-Trace: news.xtra.co.nz 961077660 203.96.152.26 (Fri, 16 Jun 2000 02:01:00 NZST)
NNTP-Posting-Date: Fri, 16 Jun 2000 02:01:00 NZST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I expect your problem is with the flushing of standard output:  in C or C++
standard output is only displayed to the screen every now and then, usually
when a new line is reached.  This is called buffered output, and is most
obvious when the output is interspersed with input from the user.

To flush standard output in C++ try something like:
    cout << flush;
Or in C:
    fflush (stdin);

"Chris McKenzie" <taganov AT earthlink DOT net> wrote in message
news:tX425.32454$hp4 DOT 768849 AT newsread1 DOT prod DOT itd DOT earthlink DOT net...
>     The question I have requires a little explanation first.  I am taking
> C++ in college right now--and I was given some excercise program to write,
> but it was hard to tell if my output was correct because the screen would
> simply flash the output and then put me back to the code screen.  I came
up
> with the idea of putting an extra "cin" command right before my return 0;
> in "int main()" so that just prior to finishing, the program stops and
lets
> me look at the output.  This works just fine.
>     Then I was reading in my textbook about how you can include "conio.h"
to
> look for a keyboard hit.  So I wrote a for-loop to replace my cin
statement.
> The for-loop looks for a keyboard hit prior to returning 0, and just
waits.
> I can hit any key--the for loop condition is met, and I return 0 and exit
> the program. Here is an example:
>
> #include <iostream>
> #include <conio.h>           file://include so that program will maintain
>                              file://user window until I press a key on
>                              file://the keyboard
> using namespace std;
> int dummy;
>
> int main()
> {       int test, num, accum;
>         test = 0;
>         accum = 0;
>
>         cout << "Please enter a number, and I will give you its
>              sum of squares   \t";
>         cin >> num;
>
>         while(test < num){
>              test++;
>              accum = accum + (test * test);}
>
>         cout << "The sum of squares for " << num << " is " << accum << ".
> \n";
>
>         for(dummy = 1; !kbhit(); dummy++);
>         return 0;}
>
> This program compiles and executes perfectly.  However, the following
> program illustrates my problem perfectly:
> #include <iostream>
> #include <conio.h>
> using namespace std;
>
> int main()
> {
>         int integer1, integer2, result;
>         char operation;
>
>  //simple calculation program
>
>         cout << "Please enter a number.   \n";
>         cin >> integer1;
>         cout << "\n Please enter an operator (+, -, *, /). \n";
>         cin >> operation;
>         cout << "\n Please enter a second number \n";
>         cin >> integer2;
>
>  //checks to make sure a correct operator is given before doing
calculations
>
>         if (operation)
>            {if (operation == '+')
>            {result = (integer1 + integer2);
>            cout << integer1 << " " << operation << " "
>                 << integer2 << " = " << " " << result;}
>
>            else if(operation == '-')
>            {result = (integer1 - integer2);
>            cout << integer1 << " " << operation << " "
>                 << integer2 << " = " << " " << result;}
>
>            else if(operation == '*')
>            {result = (integer1 * integer2);
>            cout << integer1 << " " << operation << " "
>                 << integer2 << " = " << " " << result;}
>
>            else if(operation == '/')
>            {result = (integer1 / integer2);
>            cout << integer1 << " " << operation << " "
>                 << integer2 << " = " << " " << result;}
>
>            else
>            {cout << "Invalid Operator!  No Operations Performed!";}
>            }
>         else
>            {cout << "Invalied Operator!  No Operations Performed!";}
>
>         int i;
>         for(i=0; !kbhit(); i++);
>         //cin >> operation;
>         return 0;
>         }       // close main function
>
> In the second program--the order of program execution seems to be messed
up.
> It goes through the If-then sequence just fine--but it doesn't cout the
> output until AFTER the kbhit condition is met.  Given that the cout
> statement is INSIDE the curly braces INSIDE the else-if conditions, this
> doesn't make any sense to me.  I know that Microsoft's Visual C++ has some
> kind of prioritization between gets() and puts(), cin and cout such that
one
> command always gets executed first regardless of the actual sequence that
> they occur in the code.  I wonder if perhaps DJGPP has some similar kind
of
> compilation algorithm that causes it to execute my for loop before
> completing the if then structure.  Any ideas?
>
> Thanks in advance;
>
> Chris McKenzie
>
>


- Raw text -


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