Variable being used without being initialised even though it is

Hi all, I have just begun learning c++, and am going through some example code with regards to linear search.. When I execute the code below, I get the error "Run Time Check Failure #3: The variable 'ar' is being used without being initialised"..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

int main()
{ 
	
	int num;
	int ar[17]={7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8};//Initialized here// 
	cout<<"List = 7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8 "<<endl;
	cout<<"Enter an integer in the list to search for: ";
	cin>>num;
	for(int i=0; i<(ar[17]-1); ++i) //Would like to know if this test is    possible or not? And this is where I'm getting the error of not being initialised//
	{

		if(num==ar[i])     
		{
			cout<<"Item found at index ["<<ar[i]<<"]"<<endl;
		}
	}


}


As you can see, the array ar[17] is already initialised before... Also if there are any mistakes in the code or if this can be written in a better way, please let me know :)... Thanks in advance..
It works fine for me, but give your array one more slot for the NULL value, you should always give it more room.

int ar[18] = ...

return 0; to main
Thanks for the reply... I tried what you suggested, but I still get the same error..What program are you using? I'm using VS2010..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

int main()
{

	//int num;
	//int ar[17]={7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8};
	const int ARRAY_SIZE = 17 ;
	const int ar[ARRAY_SIZE] = {7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8};

	//cout<<"List = 7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8 "<<endl;
	for( int i = 0 ; i < ARRAY_SIZE ; ++i ) std::cout << ar[i] << ' ' ;
	std::cout << '\n' ;

	cout<<"Enter an integer in the list to search for: ";
        int num ; cin >> num ;

	//for(int i=0; i<(ar[17]-1); ++i)
        for( int i = 0 ; i < ARRAY_SIZE ; ++i )
       {
		if( num == ar[i] )
		{
			//cout<<"Item found at index ["<<ar[i]<<"]"<<endl;
			cout<<"Item found at index [" << i << "]\n" ;
		}
	}
}
Last edited on
Yeah your mistake was in the for loop.

for(int i=0; i<(ar[17]-1); ++i)

calling ar[17] will give you the value inside of it

ar[17] is not initalized. the size of the array is 17, but the index starts with 0 and hence you can only use index 0...16 (which covers the 17 values!)

Even if you use ar[16] the conent is 8. There's no use of it.
Thank you JLBorges.. That was helpful and saved my hair! :)..
coder77 I'm not getting what you said.. Variable initialisation is when you assign the values to the variable, right? So in this case, I assigned the set of values to the array.. Specifically I didnt understand this
Even if you use ar[16] the conent is 8. There's no use of it.


When you initialized the array with ar[17] the compiler will give you the range ar[0] - ar[16] only. So when you called ar[17] it doesn't exist.

HTH
oh..ok.. Thanks for clarifying.. :)
ar[16] addresses the field of the array with the index 16 (the last available field of this array. (0 is the first index. Don't forget that!). The content of this field is 8 because you initilized it as such.

You can write the initialization of you your array differently:
1
2
3
4
	const int ar[] = {7,3,32,2,55,34,6,13,29,22,11,9,1,5,42,39,8};
	const int ARRAY_SIZE = sizeof(ar) / sizeof(ar[0]); // sizeof(ar) is the size of the array in bytes
		// sizeof(ar[0]) is the size of a single fields in bytes
		// = the number of fields 
That cleared up a lot of things for me.. :)... btw, in case of a user defined array, where the content of the array is not hardcoded as such, can I still use
const int ARRAY_SIZE = sizeof(ar) / sizeof(ar[0]); , since the user may enter n number of fields, will this code hold up for that scenario? And thanks once again.. :)
> btw, in case of a user defined array ... since the user may enter n number of fields,
> will this code hold up for that scenario?

Under that scenario, the use of a C-style array is contraindicated.
Use std::vector<> instead.
http://www.cprogramming.com/tutorial/stl/vector.html

since the user may enter n number of fields, will this code hold up for that scenario?
Nope, for dynamic arrays you need to keep n
Thanks JLBorges..
Topic archived. No new replies allowed.