Sequential search in 1D-array

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. So far I know that my sequential search function needs these inputs parameters : the 1D array which has all the data, the search key, and the size of the array. Here is what I have so far:

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

int seqsearch(int codes[], int target, int n);


int main()
{
int pin(0);
int codes [300];
ifstream myfile;
myfile.open ("SystemAccessCodes.txt");


cout << "=====ENTER YOUR CODE TO ACCESS INTO THE SYSTEM=====" << endl << endl;

for(int Attempts(1); Attempts <= 3; ++Attempts)

{
cout << "Attempt 1/3 : Enter 4 digit code ";
cin >> pin;

}

system("pause");
return 0;
}

As you can see, I really am just a beginner and the code above is incomplete and I deleted about half of the code because it was so disorganized. I'm just confused on how to make the program read the data file how to make the sequential search function.
you are facing a few problems but maybe you can run into those issues and ask the questions. for now you need to know when the code will end.

solution for a 300 code list
when you add the codes from the file put a marker at the last element maybe say codes[300] = 33; /* all other codes are 4 digits so some two digit number to know that it is at the end */

then loop
//you could use a boolean and then return true or false if it is found or not
n = 301; //largest index to know that the code was not found
for(int i = 0; codes[i] != 33; i++){
if(codes[i] == target){
//it is found if it reaches this part
n = i;
break;
}
}
//if n is not found then it will rerturn a 301 that is outside the range of codes
//if n is found it will return the number
return n;
Thanks for replying. I read your post and I understand what you did but I'm still confused on how to organize the program and make it read the text file.
As to reading the file, can't you just do something like this?

1
2
3
for(int i = 0;i<300;i++){
myfile>>codes[i];
}


After you read in the code values to your array, you can just compare the target to the code values like page3131 said.
Thanks I think I am slowly starting to piece this together. So far I have:

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

int seqsearch(int codes[], int target, int n);
int main()
{
int pin(0);
int codes[300];
ifstream myfile;
myfile.open ("SystemAccessCodes.txt");

for(int i = 0;i<300;i++)
{
myfile>>codes[i];
}

cout << "=====ENTER YOUR CODE TO ACCESS INTO THE SYSTEM=====" << endl << endl;

for(int Attempts(1); Attempts <= 3; ++Attempts)
{
cout << "Attempt 1/3 : Enter 4 digit code ";
cin >> pin;
cout << "Access granted" << endl;
}


system("pause");
return 0;
}

// The sequential search function
int seqsearch(int codes[], int target, int n)
{
n = 301;
for (int i=0; codes[i] != 33; i++)
{
if (codes[i] == target)
{n = i;
break;}
}
return n;

}

Now I this is where I get stuck: At the top where it says "int codes[300];" am I supposed to set that equal to something because I don't think this is right. Also, where it says "for(int Attempts(1); Attempts <= 3; ++Attempts)" how can I make the program show "Access denied" or "Try again" if the attempt at entering the 4 digit code is wrong? (I'm guessing an if-else structure would be suitable?)
This is what everyone has been saying:
for(int i = 0;i<300;i++){
myfile>>codes[i];
}


Yes, an if/then statement would work obviously. Lol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cin >> pin;
if(pin != codes[300]){
     if(attempt < 3){
          cout << "Try Again.\n";
          attempt ++;
          }
   
     else if(attempt == 3){
          cout << "Terminal locked.\n";
          }
     }

else{
     //Reminded me of a very easy terminal in Fallout 3.  Lol.
     cout << "SUCCESS +38 EXP\n";
     }


Make the "For" statement into a "While" statement as well and another "int".

1
2
3
4
5
6
7
8
9
10
11
int hacked = 0;

while(hacked == 0 || attempts != 3){
     ...
     ...
     else{
          //Reminded me of a very easy terminal in Fallout 3.  Lol.
          cout << "SUCCESS +38 EXP\n";
          hacked = 1;
          }
     }
Last edited on
Thanks for responding. I did what you said, but I ran into a couple of problems when I ran the program. First, it tells me that "attempts" and "attempt" are undeclared identifiers so I made an "int attempt" and "int attempts" but that didn't seem to help. Then for some reason even if I enter the correct 4 digit password it still tells me I'm wrong. Any ideas?
Topic archived. No new replies allowed.