error in turbo sort

#include<stdio.h>
using namespace std;
int main()
{
long t;
long *a = new long[t];

scanf("%ld",&t);

for(long i=0;i<t;i++)
	scanf("%ld",&a[i]);

for(i=1;i<=t-1;i++)
{
	for(long j=0;j<=i-1;j++)
		if(a[j]>a[i])
			break;
	if(j<=i-1)
	{
		long temp = a[i];
		for(long k=i-1;k>=j;k--)
			a[k+1]=a[k];
		a[j]=temp;
		}
	}

for(i=0;i<t;i++)
	printf("%ld\n",a[i]);

return 0;
} 

pls tell why are these errors coming?

prog.cpp: In function ‘int main()’:

prog.cpp:13: error: name lookup of ‘i’ changed for new ISO ‘for’ scoping

prog.cpp:10: error: using obsolete binding at ‘i’

prog.cpp:18: error: name lookup of ‘j’ changed for new ISO ‘for’ scoping

prog.cpp:15: error: using obsolete binding at ‘j’

These errors are coming because once you declare any variable within a for statement, it has a local scope, restricted to only the body of for. If u want to use i & j outside the for statement, either declare them again, or declare them OUTSIDE the for statement, so that they can be accessed from anywhere within main.

1 Like

as you declare int or long inside loop, there scope is limited only for that loop.

So if you want to remove these errors initialize i and j as long int (in your case) above all for loop.

And your mentioned errors will not be there… :slight_smile: