Hey everyone,
I am trying to write a program that will restrict access to unauthorized users. ive gone through my textbook several times and have scanned just about every forum out there and haven't had much luck trying to find the answer to my questions. i have a seperate .txt document that includes 300, four digit pin numbers; the program must read from the document, and determine whether a user is authorized to access the system based off of their input (pin number). if the user has three failed attempts (incorrect pin number), the program should end, if they enter the correct pin number, then they are granted access. my assignment mentions sequential search of the files and placing the data into a 1-D Array but i am at a loss as to how to apply those as my understanding of them is very limited. thank you all for your help. i placed notes inside of the code so that you can see the areas i am having issues with.
#include <iostream>
#include <fstream>
usingnamespace std;
int seqsearch(int codes[], int target, int n);
int main()
{
int pin(0);
int x = 0;
int codes[300];
ifstream myfile;
myfile.open("SystemAccessCodes.txt");
if (myfile.is_open)
for (int i = 0;i < 300;i++)
{
myfile >> codes[i];
//is this the right way to approach this?
}
cout << "=====ENTER YOUR CODE TO ACCESS INTO THE SYSTEM=====" << endl << endl;
while (x < 4)
//The user is granted three attempts to log in, if they exceed 3
//the program is supposed to end, i know this section is messy,
//but i am at a loss as to how i am supposed to accomplish this
{
{
// as the user enters their pin, the cout should change from 1/3 attemps
// to 2/3 attemps, and so on until they max out at 3, how do i go about
// accomplishing this
cout << "Attempt"<<x<<"/3 : Enter 4 digit code ";
cin >> pin;
// i am not sure how to set up my cin so that it reads completely through
// my SystemAccessCode.txt to detrmine if the user is entering a correct pin
//number while also accounting for their number of attempts
x = x + 1;
if (x==1 )
{
cout << "Not Matching, Try Again \n";
x++;
}
elseif (x > 3)
{
cout << "Access Denied \n";
cout << "Bye";
}
else
{
cout << "access granted, welcome";
}
}
}
system("pause");
return 0;
}
Thank you for taking the time to respond, @furryguy , could you possibly give me some insight as to how i how i would read my file into a vector, im kind of confused as to how i would go about that. im extremely new to all of this by the way, and my book has started to look like hieroglyphics at this point.
You are already reading your file into an array. Change the code to read each data record into a temp variable and then use std::vector::push_back() to add the record at the end of the vector.
http://www.cplusplus.com/reference/vector/vector/push_back/
There are std::vector methods to assist in looping through a vector. Spending some time learning what a container class can do for you is a good thing. :)
http://www.cplusplus.com/reference/vector/vector/
I have made a few updates to the program based off of your recommendations and various other forum posts to try and clean it up but i think i may have done opposite of that. im still having issues updating the attempt count as the user continues, and im not quite sure if im any closer to being able to read the data file for the correct pin. any suggestions as to where i should go from here?
p.s. im still at a loss for the vector portion and how to apply it in this case
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
usingnamespace std;
int seqsearch(int codes[], int target, int n);
int main()
{
int pin, i=0,ii=0;
int x = 0;
int codes[300];
int flag = 0;
string s;
ifstream myfile;
myfile.open("SystemAccessCodes.txt");
while (getline(myfile, s))
{
cout << "=====ENTER YOUR CODE TO ACCESS INTO THE SYSTEM=====" << endl;
cout << "Attempt" << x << "/3 : Enter 4 digit code ";
cin >> pin;
x = x + 1;
//im still at a loss as to how i have my cout update from: 1/3-2/3-3/3 (attempts)
for (int i = 0;i < 300;i++)
{
{
myfile >> codes[i];
// as for pushing my code into a vector, im still not sure how to go about that
// i have read both of the links and am still at a loss
}
if (codes[i] == pin)
{
flag = 1;
ii = i;
break;
}
}
if (flag == 1)
{
cout << "Access Granted" << endl;
cout << "welcome" << endl;
}
else
{
cout << "Access Denied" << endl;
cout << "Bye";
}
}
system("pause");
return 0;
}