skip to Main Content

The compiler can’t handle even the simplest loop

#include <iostream>
using namespace::std;

int main()
{

    for( int i = 0, char a = 'A'; i <= 26; i++, a++ )

    cout << "OK, lets try. Showing values: i = "
         << i << ", a = " << a << endl;
}

Compiler says this:

prog.cpp: In function ‘int main()’:
prog.cpp:7:18: error: expected unqualified-id before ‘char’ 
prog.cpp:7:18: error: expected ‘;’ before ‘char’ 
prog.cpp:7:39: error: expected ‘)’ before ‘;’ token 
prog.cpp:7:41: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive] 
prog.cpp:7:41: note: (if you use ‘-fpermissive’ G++ will accept your code) 
prog.cpp:7:46: error: ‘a’ was not declared in this scope 
prog.cpp:7:50: error: expected ‘;’ before ‘)’ token

And yes, I know I can initialize ‘i’ and ‘a’ before the loop. So let’s try:

#include <iostream>
using namespace::std;

int main()
{
    int i = 0;

    for(i = 0, char a = 'A'; i <= 26; i++, a++ )

    cout << "OK, lets try. Showing values: i = "
         << i << ", a = " << a << endl;
}

Compiler says:

prog.cpp: In function ‘int main()’:
prog.cpp:8:13: error: expected primary-expression before ‘char’
prog.cpp:8:13: error: expected ‘;’ before ‘char’
prog.cpp:8:41: error: ‘a’ was not declared in this scope

When option -std=c++11 used:

prog.cpp: In function ‘int main()’:
prog.cpp:7:17: error: expected unqualified-id before ‘char’
prog.cpp:7:17: error: expected ‘;’ before ‘char’
prog.cpp:7:38: error: expected ‘)’ before ‘;’ token
prog.cpp:7:40: error: ‘i’ was not declared in this scope
prog.cpp:7:45: error: ‘a’ was not declared in this scope
prog.cpp:7:49: error: expected ‘;’ before ‘)’ token

Last one:

#include <iostream>
using namespace::std;

int main()
{
    int i = 0;
    char a = 'A';

    for(i = 0, a = 'A'; i <= 26; i++, a++ )

    cout << "OK, lets try. Showing values: i = "
         << i << ", a = " << a << endl;
}

Works fine. Guys, am I blind or something? Maybe you need my arch and compiler version:

uname -a
Linux freelancer 3.2.0-4-686-pae #1 SMP Debian 3.2.63-2+deb7u2 i686 GNU/Linux
g++ --version 
g++ (Debian 4.7.2-5) 4.7.2

2

Answers


  1. You cannot declare items of different types in the same declaration.

    This is true inside and outside of loops. You’re not “blind”, it’s just not valid C++.

    Here’s the right code:

    #include <iostream>
    using namespace::std;
    
    int main()
    {
        int i = 0;
        char a = 'A';
    
        for(; i <= 26; i++, a++ )
    
            cout << "OK, lets try. Showing values: i = "
                << i << ", a = " << a << endl;
    }
    

    Your working version is also valid because the declaration can be swapped out for an expression, though in your case it’s redundant because those variables already hold those values at the start of the loop.

    Login or Signup to reply.
  2. 26 is so tiny number, thus you can do

    #include <iostream>
    
    int main()
    {
        for( char i = 0, a = 'A'; i <= 26; i++, a++ )
            std::cout << "OK, lets try. Showing values: i = "
                << static_cast<int>(i) << ", a = " << a << std::endl;
    }
    

    Or even IMHO more clear code, more clear aim, iterating from ‘A’ until ‘Z’.

    int main()
    {
        for( char i = 0, a = 'A'; a <= 'Z'; i++, a++ )
            std::cout << "OK, lets try. Showing values: i = "
                << static_cast<int>(i) << ", a = " << a << std::endl;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search