3 attempts for access code. sequential search

Hello everyone it's nice to meet you all this is my first time posting on here.
I'm having a little trouble with developing a c++ code. Basically I have to make a program that prompts the user to enter a 4 digit code and then the program reads the 4 digit code and searches the data file for a match (The data file is a text file that contains 300 random four digit-codes.) The thing is that the program only allows 3 attempts and after each attempt their should be an output that says "Access granted" for successful attempt, "Try again" if the attempt is wrong, and "Access denied" if all 3 attempts were wrong. Also i have to writea function for the sequential search. My program seem to work fine, except when the user put a right access code the program doesn't exit but instead ask for another access code in the next attempt. I tried to do ( return 0; ) to exit from the function, it didn't work. Please help

#include<iostream>
#include<fstream>
using namespace std;


void mySequentialSerach(double[], int, int);

int main() {

const int N = 300;
int npts = 0;
double y[N], code;
ifstream input_file;
int key;
int flag = 0;

for (int attempts = 0; attempts <= 2; attempts++)
{
if (attempts == 0)
cout << " Attempt 1/3 : ENTER 4 DIGIT CODE " << endl;


else if (attempts == 1)
cout << "attempt 2/3 : ENTER 4 DIGIT CODE" << endl;

else if (attempts == 2)
cout << " attempt 3/3: ENTER 4 DIGIT CODE" << endl;


cin >> key;

input_file.open("code.txt");
if (input_file.fail()){
cerr << "error opening file" << endl;
exit(1);
}
input_file >> code;


while (npts < N && !input_file.eof())

{
y[npts] = code;
++npts;
input_file >> code;

}

input_file.close();

mySequentialSerach(y, N, key);


}
cout << "ACCESS DENIED" << endl;
cout << "BYE" << endl;


system("pause");
return 0;

}

void mySequentialSerach(double y[], int N, int key)
{
int flag = 0;
for (int i = 0; i < 300; i++)
{
if (y[i] == key)
{
flag = 1;

break;
}
}
if (flag == 1)
{
cout << "ACCESS GRANTED" << endl;
cout << "WELCOME" << endl;
return 0;
}

else
cout << "NOT MATCHING ! TRY AGAIN" << endl;


}
Ill will post my version of the code in 10 minutes.
Just curouse where you are in coding have you learned arrays?
Last edited on
Does the program need to stop after the 3rd attempt? and Outputting access denied?
The program will give a user only 3 attempts. if the user put a wrong code 3 times in a row the program will end. My only problem is when the user put a correct access code the "ACCESS GRANTED" appears and then "attempt 2/3" appears as well. I want the program to exit after the user put a correct access code just one time. i tried to use return 0; in the function but it didn't allow. i also tried return; and return void (); none of them let the program exit if it finds the right code.
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <fstream>
using namespace std;

int mySequentialSearch(const int digitCode);

int main() {

    const int MAX_ATTEMPT = 3;
    int countAttempts = 1;
    int digitCode;
    int validCode;
    bool found;


    while(!found)
    {

        cout << "Enter a 4 Digit Code: ";
        cin  >> digitCode;

        //Calls the mySequentialSearch function a returns the value 0 or the 4 digit code if found
        //and assigns it to validCode
        validCode = mySequentialSearch(digitCode);


        if (validCode != 0)
        {
            cout << "Access Granted\n";
            found = true;
        }
        else if(countAttempts == MAX_ATTEMPT)
        {
            cout << "Acess Denied\n";
            found = true;
        }
        else if(validCode == 0)
        {
            cout << "Try Again Attempt " << countAttempts << "/3 \n";
        }


        countAttempts++;
    }//END WHILE


    cout << "\nThe Program Has Ended! :)";
    return 0;
}

//Changed your function to value returning function of 0 or a found 4 digit code

int mySequentialSearch(const int digitCode)
{
    ifstream inFile;
    inFile.open("code.txt");

    int MAX_CODES = 10;                  //In your case change to 300. I had a .txt file of 10
    int textFileCode[MAX_CODES];
    int index = 0;
    int instances = 0;
    int foundValue = 0;
    bool found = false;

    //Input Entire text file into an array
    while(inFile && index < MAX_CODES)
    {
        inFile >> textFileCode[index];
        index++;
    }

    //Search through the array to find a matching 4 digit code
    //IF 4 digit code is found it will send the 4 digit code back to main
    //If 4 digit code is NOT found will send the value of 0 back to main
    while(instances < MAX_CODES & !found)
    {
        if(textFileCode[instances] == digitCode)
        {
            found = true;
            foundValue = textFileCode[instances];
        }
        else
        {
            instances++;
        }
    }

    inFile.close();

    return foundValue;

}



I rewrote your code in a way that I would write it and tested it with a simple text file
of 10 4 digit codes

1
2
3
4
5
6
7
8
9
10
1234
4321
6543
3234
6784
1111
5643
1234
3212
3432



OUTPUT 1

1
2
3
4
5
6
7
8
Enter a 4 Digit Code: 12
Try Again Attempt 1/3 
Enter a 4 Digit Code: 0000
Try Again Attempt 2/3 
Enter a 4 Digit Code: 4545
Acess Denied

The Program Has Ended! :)


OUTPUT 2

1
2
3
4
5
6
7
8
Enter a 4 Digit Code: 12
Try Again Attempt 1/3 
Enter a 4 Digit Code: 0000
Try Again Attempt 2/3 
Enter a 4 Digit Code: 5643
Access Granted

The Program Has Ended! :)



Last edited on
Your code is all over the place and not very readable let alone hard to debug.
There are many ways to write this program but I wrote this program in 15 minutes
and it is very structured and easy to follow.

Let me know if you have any questions.
@alleyezonne

lines 66-70: Do not loop on (infile). This does not work the way you expect. The eof and fail bits are set true only after you make a read attempt on the file (line 68). This means after you read the last record of the file, eof iand fail are still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed to increment index as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
66
67
68
  while (infile >> texfFileCode[index] && index < MAX_CODES) 
  {  index++;   // Executed only if >> operation succeeded
  }
Thank you soooo much for the help. the program works fine.
Topic archived. No new replies allowed.