Hi,

I am having problem in compiling the following piece of simple code(it didn't work in any of the compilers VC++,GCC etc)

#include<iostream>
using namespace std;

int main()
{
 	while((char c = cin.get()) != 'q')
		cout<<endl<<"Character printed = "<<c<<endl;
	
 }

ERRORS: expected primary-expression before "char"
expected `)' before "char"
expected `)' before ';' token

but the following works:

#include<iostream>
using namespace std;

int main()
{
 	while(char c = cin.get() != 'q')
		cout<<endl<<"Character printed = "<<c<<endl;
	
 }

I got the same messages as you did when I tried to compile. It compiled and ran for me when I took the "char c" declaration outside of the while loop, like this:

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

Possible the problem is that when c is defined inside those parentheses, it is going out of scope too soon? Just a guess.

Hi,

but the following works:

#include<iostream>
using namespace std;

int main()
{
    while(char c = cin.get() != 'q')
        cout<<endl<<"Character printed = "<<c<<endl;

} 

end quote.

I actually get happy faces when I run the above. I think it's printing some control character.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

> the following piece of simple code(it didn't work in any of the compilers VC++,GCC etc)
the code is incorrect and should not compile without an error. this is (a simplified version) of what the standard says about while loops:

in while(condition) substatement the condition is one of
i. expression
ii. type-specifier-seq declarator = assignment-expression

the declarator shall not specify a function or an array. the type-specifier-seq shall not contain typedef and shall not declare a new class or enumeration.

the value of a condition that is an expression is the value of the expression, implicitly converted to bool; if that conversion is ill-formed, the program is ill-formed.
the value of a condition that is an initialized declaration is the value of the declared variable implicitly converted to bool; if that conversion is ill-formed, the program is ill-formed.

if a condition can be syntactically resolved as either an expression or the declaration of a local name, it is interpreted as a declaration.

The object created in a condition is destroyed and created with each iteration of the loop.
Example:

struct A 
{
    int val;
    A(int i) : val(i) {}
    ~A() {}
    operator bool() { return val != 0; }
};
int i = 1 ;
while( A a = i ) { /* ... */ i = 0 ; }

in the while-loop, the constructor and destructor are each called twice, once for the condition that succeeds and once for the condition that fails.

the problem with while((char c = cin.get()) != 'q') is easy enough to see;
what is attempted is an expression of the form e1 != e2. char c = cin.get() is not an expression.
you would also get the same error with

int main()
{
  char a = 'a' ; // ok
  ( char b = 'b' ) ; // error: expected primary expression
  int i ;
  for( i=0 ; i<10 ; ++i ) ; // ok
  ( for( i=0 ; i<10 ; ++i ) ) ; // error: expected primary expression
}

> but the following works:
it compiles, but the result may not be what you expect while( char c = cin.get() != 'q' ) is semantically equivalent to while( char c = ( cin.get() != 'q' ) ) c will be either char(true) or char(false)

Thanks Vijayan!! Hey can you suggest me some links,tuts etc...which further illustrate this point!!!

This article has been dead for over six months. Start a new discussion instead.