Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Synax error - Rule of parentheses.

2 views
Skip to first unread message

Memana

unread,
Jul 7, 2004, 4:01:21 AM7/7/04
to
Dear Friends,
I posted a message related to this earlier.
Nobody replied. so asking the same question.

Why following program cuase compilation error in VC++?

//--------------------------------------------------------//
#include<iostream>

using namespace std;

int main()
{

while((char c == cin.get()) != 'q')
cout<<c<<endl;

return 0;
}
//--------------------------------------------------------//

error C2146: syntax error : missing ')' before identifier 'c'
error C2065: 'c' : undeclared identifier
error C2143: syntax error : missing ';' before '!='
error C2059: syntax error : ')'

I know that there is "some" constrainst for the Parentheses. But does
anybody knows the rule of Parenthesis for while(), if() and switch?

Thanks,
Meman A

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Alf P. Steinbach

unread,
Jul 7, 2004, 1:40:58 PM7/7/04
to
* Memana:

> Dear Friends,
> I posted a message related to this earlier.
> Nobody replied. so asking the same question.

Dear Memana. I answered this for you first time you posted the
question in [comp.lang.c++], before posting it here.



> Why following program cuase compilation error in VC++?
>
> //--------------------------------------------------------//
> #include<iostream>
>
> using namespace std;
>
> int main()
> {
>
> while((char c == cin.get()) != 'q')
> cout<<c<<endl;
>
> return 0;
> }

Because the syntax for a 'condition' defines only two allowable forms:

expression

or

type-specifier-seq declarator = assignment-expression


And as I also wrote then, questions as to _why_ the syntax is as it is
are best posed in [comp.std.c++].

However, I'll make an attempt. If the syntax were to allow variable
declarations inside parentheses it would be hard to not allow that in
general, and so one question would be: what scope should those variables
have? In your example clearly the scope extends outside the parenthesis.
So perhaps the enclosing scope.

But then we have degenerated further, from C only allowing declarations
at the top of that scope, to C++ allowing declarations anywhere inside
the scope, although not in expressions, to now also allowing declarations
inside any expression -- and those declarations might be hard to spot...


> I know that there is "some" constrainst for the Parentheses. But does
> anybody knows the rule of Parenthesis for while(), if() and switch?

That's now been answered twice by me, and also by others.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Michael Lund

unread,
Jul 7, 2004, 1:44:09 PM7/7/04
to

> while((char c == cin.get()) != 'q')

I think a single = for assignment to c should be used here.
I am not sure you are allowed to declare c in this place. Try:

char c;
while ((c=cin.get())!='q')
{
....
}

Michael

Francis Glassborow

unread,
Jul 7, 2004, 1:56:52 PM7/7/04
to
In article <c0164811.04070...@posting.google.com>, Memana
<srme...@yahoo.com> writes

>Dear Friends,
> I posted a message related to this earlier.
> Nobody replied. so asking the same question.
>
>Why following program cuase compilation error in VC++?
>
>//--------------------------------------------------------//
>#include<iostream>
>
>using namespace std;
>
>int main()
>{
>
> while((char c == cin.get()) != 'q')
> cout<<c<<endl;
>
> return 0;
>}
>//--------------------------------------------------------//
>
>error C2146: syntax error : missing ')' before identifier 'c'
>error C2065: 'c' : undeclared identifier
>error C2143: syntax error : missing ';' before '!='
>error C2059: syntax error : ')'
>
>I know that there is "some" constrainst for the Parentheses. But does
>anybody knows the rule of Parenthesis for while(), if() and switch?

The grammar of the language for a condition (see 6.4) allows a choice of
either an expression or a very specific form of declaration. There is no
production that allows a declaration as part of an expression. Note that
this is no different from trying to write:

(char c == cin.get()) != 'q';

Declarations do not work like that in C++ (is there any language in
which they do?)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

Thomas Richter

unread,
Jul 7, 2004, 2:01:29 PM7/7/04
to
Hi,

> Why following program cuase compilation error in VC++?

Because it's invalid C++. (-;

> //--------------------------------------------------------//
> #include<iostream>

> using namespace std;

> int main()
> {

> while((char c == cin.get()) != 'q')
> cout<<c<<endl;

1) Are you sure you want == here and not = ?
2) A definition is a statement, not an expression, and parenthesis
group expressions. The parenthesis of "if","while" and "for" are not
for grouping expressions, though, and thus follow another rule.

So long,
Thomas

Ulrich Eckhardt

unread,
Jul 7, 2004, 2:03:21 PM7/7/04
to
Memana wrote:
> Why following program cuase compilation error in VC++?
[...]
> while((char c == cin.get()) != 'q')
> cout<<c<<endl;

you probably meant this:
while( (char c = cin.get()) != 'q')
...

However, this still won't compile, you have to move the declaration out of
the loop:


char c;
while((c=cin.get()) != 'q')

...

Now, this will compile but still not do what my Crystal Ball(tm) told me you
want to. If you press ctrl-G(I think, on Unix and similar systems it is
ctrl-D), the stream gets an eof. In your case, get() returns a special
value and the streamstate turns to 'fail', and from that point on your
programm will run in an endless loop until it is killed somehow.
Therefore, take the other overload of get():
char c;
while( cin.get(c) && (c!='q'))
...

One last note: cin is usually linebuffered, meaning you can type a lot but
it is only 'sent' when you finish a line. If you thought you could 'watch'
the user press single keys, I have to disappoint you, that is not possible
in C++ without system-specific extensions.

Uli


--
FAQ: http://parashift.com/c++-faq-lite/

/* bittersweet C++ */
default: break;

Mike Spencer

unread,
Jul 7, 2004, 9:40:11 PM7/7/04
to
Memana wrote:
> Why following program cuase compilation error in VC++?
> ...

> while((char c == cin.get()) != 'q')
> cout<<c<<endl;
>
> ...

> error C2146: syntax error : missing ')' before identifier 'c'
> error C2065: 'c' : undeclared identifier
> error C2143: syntax error : missing ';' before '!='
> error C2059: syntax error : ')'
>
> I know that there is "some" constrainst for the Parentheses. But does
> anybody knows the rule of Parenthesis for while(), if() and switch?

A "condition", an expression or a declaration with an initializer,
must be inside the parenthesis of an if, switch, and while statement.
From the C++ grammar:

statement:
IF ( condition ) statement
IF ( condition ) statement ELSE statement
SWITCH ( condition ) statement
WHILE ( condition ) statement
...

condition:
expression
type-specifier-seq declarator = assignment-expression

Your while statement is invalid because the left operand to !=, (char
c = cin.get()), is not an expression. The declaration must be at the
top level; it can not be embedded within an expression.

So parenthesis can actually be used to force a declaration to be an
expresssion (so I suppose the rule makes sense :)

struct A {
A (int);
bool operator = (int);
operator bool ();
};

void f (int b) {
if (A (b) = 1) {} // declaration
if ((A (b) = 1)) {} // expression
}

(Comeau parses this correctly, gcc 3.2.3 does not.)

--
Lzz: The Lazy C++ Programmer's Tool
http://www.lazycplusplus.com

Lukas Mai

unread,
Jul 8, 2004, 8:10:16 AM7/8/04
to
Francis Glassborow schrob:
[...]

> The grammar of the language for a condition (see 6.4) allows a choice of
> either an expression or a very specific form of declaration. There is no
> production that allows a declaration as part of an expression. Note that
> this is no different from trying to write:

> (char c == cin.get()) != 'q';

> Declarations do not work like that in C++ (is there any language in
> which they do?)

Yes, the following line is valid Perl code:
chomp(my $line = <STDIN>);

It declares $line, reads a line from STDIN into it, and removes the
trailing '\n' in a single statement. The scope of $line starts at the
_next_ statement, so
{
my $foo = $foo;
}
initializes the inner $foo with the outer value of $foo (another
lexical, or a global variable).

HTH, Lukas
--
/*>++++++++++++[<+++++++>-]<.>+++++[<++++>-]<.---.+++++++++++++.-------------.[
-]>++++++++[<++++>-]*/#include<stdio.h>/*+++++++++[<++++++++>-]<+.++++++++++.*/
/*[-]>++++++++[<++++>-]<*/int main(void){return puts("There is no .")*0;}/*>+++
++++++++++[<++++++>-]<.+.[-]>++++++++[<++++>-]<.++++++++++++++.[-]++++++++++.*/

ka...@gabi-soft.fr

unread,
Jul 8, 2004, 7:38:20 PM7/8/04
to
al...@start.no (Alf P. Steinbach) wrote in message
news:<40ebaf33....@news.individual.net>...

[...]


> Because the syntax for a 'condition' defines only two allowable forms:

> expression

> or

> type-specifier-seq declarator = assignment-expression

> And as I also wrote then, questions as to _why_ the syntax is as it is
> are best posed in [comp.std.c++].

> However, I'll make an attempt. If the syntax were to allow variable
> declarations inside parentheses it would be hard to not allow that in
> general, and so one question would be: what scope should those
> variables have? In your example clearly the scope extends outside the
> parenthesis. So perhaps the enclosing scope.

> But then we have degenerated further, from C only allowing
> declarations at the top of that scope, to C++ allowing declarations
> anywhere inside the scope, although not in expressions, to now also
> allowing declarations inside any expression -- and those declarations
> might be hard to spot...

I imagine that the real problem is that some of those declarations might
be conditional. Consider something like:

while ( (char c = cin.get()) != 'g'
|| (char d = cin.get() != 'x' ) {

// what about d here ?
}

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages