System Access troubleshoot

teh program i am writing is supposed to take a user inputted 4 digit code and then compare that to a list of codes given to us in a data file. I want the code to search through the data file sequentially to find a match. how would i do this? Also when i run the code i get this error "Run-Time Check Failure #3 - The variable 'code' is being used without being initialized." didnt i initialize 'code' in line 10?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>


using namespace std;

int main()
{
	int pin;
	int code[300];
	ifstream accessFile;
	accessFile.open("SystemAccessCodes.txt");

	cout << "===== ENTER YOUR CODE TO ACCESS INTO THE SYSTEM =====\n"
		 << "Attempt 1/3 : ENTER 4 DIGIT CODE: ";
	cin >> pin;

	if (pin != code[300])
	{
		cout << "DOES NOT MATCH. TRY AGAIN \n\n" << "Attempt 2/3 : ENTER 4 DIGIT CODE: ";
		cin >> pin;

		if (pin != code[300])
		{
			cout << "DOES NOT MATCH. TRY AGAIN \n\n" << "Attempt 3/3 : ENTER 4 DIGIT CODE: ";
			cin >> pin;

			if (pin != code[300])
			{
				cout << "==================================================\n"
			         << " ACCESS DENIED \n" 
			         << "    BYE\n" 
			         << "==================================================\n";
			}
			else
			{
				cout << "==================================================\n"
			         << " ACCESS GRANTED \n" 
			         << "    WELCOME\n" 
			         << "==================================================\n";
			}
		
		}
		else
		{
			cout << "==================================================\n"
			     << " ACCESS GRANTED \n" 
			     << "    WELCOME\n" 
			     << "==================================================\n";
	
		}
	}
	else
	{
		cout << "==================================================\n"
			 << " ACCESS GRANTED \n" 
			 << "    WELCOME\n" 
			 << "==================================================\n";
	}
}
`code[300]' is out of bounds.

> The variable 'code' is being used without being initialized." didnt i initialize 'code' in line 10?
¿what value does it hold?
i thought it held the 300 codes in the data file. how do i get it to read the data and determine if the user input matches. If some could explain how to take data from a data file and put it into an array i would appreciate my book does a terrible job of explaining.
Last edited on
> i thought it held the 300 codes in the data file.
¿why?
1
2
3
int size=0;
while(input>>code[size])
   ++size;


To test for membership `std::find' (traverse the entire array) or `std::binary_search' (need the array to be sorted)
i dont know i just assumed so. definitely not getting this at all
Topic archived. No new replies allowed.