Thread: simple code won't compile...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    simple code won't compile...

    Code:
    #include <iostream>using namespace std;
    int main()
    {
    
    
      int array[8];
      for(int x=0; x<8; x++)
      cin>>array[x];
      for(x=0; x<8; x++)
      cout<<array[x];
      return 0;
    }

    D:\Documents\C PROGRAMS\Chal1\main.cpp||In function 'int main()':|
    D:\Documents\C PROGRAMS\Chal1\main.cpp|9|error: name lookup of 'x' changed for ISO 'for' scoping [-fpermissive]|
    D:\Documents\C PROGRAMS\Chal1\main.cpp|9|note: (if you use '-fpermissive' G++ will accept your code)|
    ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|



    please help...
    thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    I changer the variables of the 2nd for loop to 'y' instead of 'x'

    worked


    /solved

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your "int x" in the first for loop is only visible in the head and body of that loop. So the next loop needs to also be "int x" not just "x". Alternatively, you could define "int x;" before the first loop where it will be visible to both.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To explain the references to -fpermissive in the error messages .....

    In older versions of C++ (while the C++ standard was in draft, before ratification), x was within the scope of the containing block (i.e. the main() function in your example), not just for for() statement. There is a fair body of ancient production code that relies on such things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-17-2013, 10:10 AM
  2. Why the simple code doesn't compile?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2008, 03:28 AM
  3. why can I get this simple code to compile?
    By tarotcard in forum C Programming
    Replies: 7
    Last Post: 11-10-2005, 05:20 AM
  4. Replies: 3
    Last Post: 03-07-2003, 09:06 PM