Like Tree1Likes
  • 1 Post By Meerul264

error?? name lookup of 'i' changed for ISO 'for' scoping

This is a discussion on error?? name lookup of 'i' changed for ISO 'for' scoping within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; void swap (int array[], int first_index, int second_index); int findSmallestRemainingElement(int array[], int size, int ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    error?? name lookup of 'i' changed for ISO 'for' scoping

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void swap (int array[], int first_index, int second_index);
    int findSmallestRemainingElement(int array[], int size, int index);
    
    
    int store (int array[], int size)
    {
        std::cout << "Enter the number(s) you want to store: \n";
        int values, count = size;
        for (int i=0;i<size;i++)
        {
            std::cin >> values;
            array[i] = values;
            count--;
            std::cout << "-----" << count << "----- values left to be entered.\n";
        }
    
    
        return true;
    }
    
    
    
    
    void sort(int array[], int size)
    {
        for ( int i=0; i<size ;i++)
        {
            int index = findSmallestRemainingElement(array,size,i);
            swap(array, i, index );
        }
    
    
    }
    
    
    int findSmallestRemainingElement(int array[], int size, int index)
    {
        int index_of_smallest_value = index;
        for (int i = index+1 ; i<size ; i++)
        {
            if ( array[i] < array [index_of_smallest_value])
            {
                index_of_smallest_value = i;
            }
        }
        return index_of_smallest_value;
    }
    
    
    void swap (int array[], int first_index, int second_index )
    {
        int temp = array[first_index];
        array[first_index] = array[second_index];
       array[second_index] = temp;
    }
    
    
    void displayarray (int array[], int size)
    {
        std::cout << "{";
    
    
            for (int i = 0 ;i <size ; i++)
            {
                if (i !=0)
                {
                    std::cout << ", ";
                }
            }   std::cout << array[i];
        std::cout << "}";
    }
    
    
    int main()
    {   std::cout << "How many values would you like to store? \n";
        int size;
        std::cin >> size;
    
    
        int array[size];
        store(array, size);
        sort(array,size);
        displayarray(array,size);
        return 0;
    }
    Compiler error:

    \main.cpp||In function 'void displayarray(int*, int)':|
    \main.cpp|64|error: name lookup of 'i' changed for ISO 'for' scoping|
    \main.cpp|64|note: (if you use '-fpermissive' G++ will accept your code)|
    ||=== Build finished: 1 errors, 0 warnings ===|

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Code:
            for (int i = 0 ;i <size ; i++)
            {
                if (i !=0)
                {
                    std::cout << ", ";
                }
            }   std::cout << array[i];
    First of all, this demonstrates the importance of really good indentation.
    Putting code AFTER a brace results in pretty unreadable code.
    Look carefully, does the cout array[i] run for every loop iteration, or just once when the loop exits?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    It would help if you indented your code better (and gave the line number):
    Code:
    void displayarray (int array[], int size)
    {
        std::cout << "{";
        for (int i = 0 ;i <size ; i++)
        {
            if (i !=0)
            {
                std::cout << ", ";
            }
        }
        std::cout << array[i];
        std::cout << "}";
    }
    The problem is that i is declared in the for loop, but you use it after the for loop. Some old compilers may still accept this code as being roughly equivalent to:
    Code:
    void displayarray (int array[], int size)
    {
        std::cout << "{";
        int i;
        for (i = 0 ;i <size ; i++)
        {
            if (i !=0)
            {
                std::cout << ", ";
            }
        }
        std::cout << array[i];
        std::cout << "}";
    }
    in which case there would be no error, hence the "name lookup of 'i' changed for ISO 'for' scoping" error message.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    eek... I didn't intend to put the array out of the for loop. Thanks I'll remember this mistake. Putting code after braces is bad is noted.
    Elysia likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2012, 05:42 AM
  2. Replies: 21
    Last Post: 06-24-2011, 02:57 AM
  3. C# Scoping Delinma
    By JohnLeeroy in forum C# Programming
    Replies: 20
    Last Post: 01-16-2011, 01:40 PM
  4. Still get error even if i changed a lot..
    By icefire99 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 03:31 AM
  5. scoping visibility
    By dirgni in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 03:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21