1

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
Parth
  • 2,682
  • 1
  • 20
  • 39
  • 1
    Which C elements can you put between for( and the first ;? – Costantino Grana Mar 29 '20 at 17:45
  • 1
    C++ isn't a "muck with it until it compiles" type of language. Do yourself a favor and get a good [C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Will save you a ton of time and frustration. Seriously. It also helps to familiarize yourself with [C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) first. – rustyx Mar 29 '20 at 19:14
  • I don't recommend doing C first (that's a good way to pick up what are, in C++, bad habits), but otherwise yes. – Asteroids With Wings Mar 29 '20 at 21:38

2 Answers2

3

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.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • The only problem is that those are not declarations. They are definitions. – Costantino Grana Mar 29 '20 at 17:53
  • 2
    @CostantinoGrana They are both, and for our purposes here the distinction is irrelevant. – Asteroids With Wings Mar 29 '20 at 18:10
  • You are right, since definitions are a subset of declarations. My suggestion is that we call definitions with their names, to stress that they are allocating storage (they are not extern). – Costantino Grana Mar 29 '20 at 18:24
  • @CostantinoGrana Could do, but that would really just be a distraction here, as it has absolutely nothing to do with the topic. The topic is about the _declaration_ statement used in the `for` construct; whether that declaration is also defining or not is completely besides the point. – Asteroids With Wings Mar 29 '20 at 21:35
  • @CostantinoGrana [Example showing that the declaration in `for` does not have to be defining](https://coliru.stacked-crooked.com/a/203812a958e051ee). – Asteroids With Wings Mar 29 '20 at 21:37
  • Thanks for the example (I tried that also before your message and of course it works). My pain is with the language, not with your answer. I mean that we have a term for declarations that allocate but we don't have a term for declarations that don't allocate (extern). So I (wrongly wrt the standard) always prefer "definition" when talking of them. Of course you were right from the start. – Costantino Grana Mar 30 '20 at 06:26
  • 1
    @CostantinoGrana I call those "non-defining declarations" but I've never been in a situation where I had to deploy the term - until now ;) – Asteroids With Wings Mar 30 '20 at 10:10
  • Thanks for the term! – Costantino Grana Mar 30 '20 at 14:04
  • @CostantinoGrana Just to be clear, although I've seen other people use it, I don't know how widespread it is :) – Asteroids With Wings Mar 30 '20 at 16:59
0

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;
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • You'll need to write `+i` or use a more verbose way to cause integral promotion, otherwise you're streaming a bunch of unprintable ASCII characters. Just one reason not to use `char` when you actually have something you intend to use as a number: it signals intent poorly, and it is error-prone. Yes, `char` is implemented as a number, but you should not use it as one. – Asteroids With Wings Mar 29 '20 at 18:11
  • Thanks. Missed that `i` is put for out. – 273K Mar 29 '20 at 18:13