delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/14/01:37:22

Message-ID: <387E3AF9.9E1CCE61@earthlink.net>
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
X-Mailer: Mozilla 4.7 [en] (Win95; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: variable declaration in for-header fails??
References: <387d91d5 DOT 333064 AT news DOT netcologne DOT de>
Lines: 65
NNTP-Posting-Host: 209.246.84.11
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread2.prod.itd.earthlink.net 947796736 209.246.84.11 (Thu, 13 Jan 2000 12:52:16 PST)
NNTP-Posting-Date: Thu, 13 Jan 2000 12:52:16 PST
Organization: EarthLink Network, Inc.
X-ELN-Date: Thu Jan 13 12:52:16 2000
Date: Thu, 13 Jan 2000 20:52:16 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com


Peter Karp wrote:
> 
> Hi  :-)
> 
> I have a little question, why DJGPP can't compile a program where I've
> defined a counter in the for loop (which is better, as the counter is
> "more local" then, right?).

> but fails, when I define the counter inside the loop declaration:

You have misunderstoop the comma in "int i = 1, cout << x, x++" as a
comma operator instead of a separator or declarations.  Consider this
code

#include <iostream>
using namespace std;

int main()
{
    for (int x = 1, y = 2; x <= 100; x++, y++)
        cout << "(" << x << "," << y << ") ";
    cout << endl;
    return 0;
}

Notice that this declares two variables, x and y.  Your code tries to
declare x and cout.  Any statement in the initialization must be
otherwise a legal statement (as well as not compound).  Obviously,
   int x = 1, cout << x;
is not a legal C++ statement, so cannot appear as an initializer
statement.


> 
> #include <iostream>
> using namespace std;
> 
> int main()
> {
>   for(int x=1, cout << x , x++ ;x<=100; x++)
>     cout << ", " << x;
> 
>   cout << endl;
> 
>   return(0);
> }
> 
> BTW, Borland C++ 3.1 compiles this version without an error, while
> DJGPP gives the following error message:

The BC++ is itself in error.


-- 
Martin Ambuhl	mambuhl AT earthlink DOT net

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

A thick skin is a gift from God. - Konrad Adenauer
__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com

- Raw text -


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