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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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.

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, 02: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, 04:16 PM