Stuck on a Hw assignment and could use some help

Hello all,
I have an assignment where I have to enable a user to enter four numbers and if the numbers match the data file it prints out access granted if the numbers do not match it prints out no match try again. The user has three tries then the program prints out access denied.
Also, their has to be a function with the following:
• 1-D array which has all the data,
• Search Key,
• Size of the array
Here is what I have so far:

// Definition of the Variables
#include <iostream>
#include <fstream>

using namespace std;

int mySequentialSearch()
{
int i[300];
ifstream data("SystemAccessCodes.txt");
if(!data.fail())
{
for(int x=0; x<300; ++x)
{

data>> i[300];
cout<<i[x] << endl;
}
}
else
{
cerr << "File SystemAccessCodes could not be open" << endl;
}
// data >> x;
//while(!data.eof())
// cout << x;
data.close();
return i[300];
}

int main()
{
float digits;
cout <<"ENTER 4 DIGIT CODE" << endl;
cin >> digits;
//cout << mySequentialSearch();
if(digits==mySequentialSearch())
cout << "Access Granted" << endl;
if(digits!=mySequentialSearch())
cout << "Not Matching. Try Again" << endl;
cout << "Access denied /n Bye" << endl;
return 0;
}

Any advice or help would be awesome.
I have change the top code and I have the while loop halfway working in the main. It loops but it I cant get it to stop after the third try. Any thoughts on how to do that? Also, for the top function. I know the file is being opened and I converted the string over to an int but I think its adding the whole .txt file.
Here is what I have know:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int mySequentialSearch()
{
int value;
string line;
ifstream data("SystemAccessCodes.txt");
if(data.is_open())
{
while(!data.eof())
{
getline(data,line);
//cout << line << endl;
std:string line;
int value = atoi(line.c_str());
//line >> value;
}
data.close();
}
else
cout << "File not found";
return value;

}

int main()
{
float digits;
int retry;
//cout << mySequentialSearch();
for(retry=0;retry =3;++retry)
{
cout <<"ENTER 4 DIGIT CODE" << endl;
cin >> digits;
if(digits=mySequentialSearch())
cout << "Access Granted" << endl;
if(digits!=mySequentialSearch())
cout << "Not Matching. Try Again" << endl;
}
cout << "Access denied /n Bye" << endl;
return 0;
}

Last edited on
Got the for loop to work by changing retry=3 to retry <3.
@sdailey

And, change if(digits=mySequentialSearch()) to if(digits==mySequentialSearch())

A single equal sign, will assign a value to the variable, whereas, the double equals, checks value. Think of it as saying, if(digits is equal to mySequentialSearch()).
Thank you for that. Slowly knocking out the bugs.

Still having issues getting the function to work.
Not sure how to have it read the file as an array then have it output right. Right now it is output is 0. Think that is because the return 0;

Here is what I have now:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int mySequentialSearch()
{
string line;
ifstream data("SystemAccessCodes.txt");
int value;
if(data.is_open())
{
while(!data.eof())
{
getline(data,line);
// cout << line << endl;
// int value = atoi(line.c_str());
// line==value;

}
data.close();
}
else
cout << "File not found";
return line;

}

int main()
{
float digits;
int retry=0;

for(retry=0;retry<3;++retry)
{
cout <<"ENTER 4 DIGIT CODE" << endl;
cin >> digits;
if(digits==mySequentialSearch())
cout << "Access Granted" << endl;

if(digits!=mySequentialSearch())
cout << "Not Matching. Try Again" << endl;
}
cout << "Access denied \n Bye" << endl;
return 0;
}


Just have to keep plugging away at it.

@sdailey

The way your program is right now, you don't need an array. Just an int to check against what digit you input. Here's the program, able to open the text file, read in the 4 digit number and check against what was inputted.

Good luck on the rest of your program..

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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int mySequentialSearch()
{
 int line = 0;
 ifstream data("SystemAccessCodes.txt");
 if(data.is_open())
 {
	data >> line;
	cout << line << endl; // Delete this line. Only to show present password code
	data.close();
 }
 else
	cout << "Input file not found" << endl << endl;
 return line;
}

int main()
{
 int digits,check = mySequentialSearch();
 int retry=3;

 while(retry>0 && check > 0)
 {
	cout <<"ENTER 4 DIGIT CODE" << endl;
	cin >> digits;
	
	if(digits==check)
	{
	 cout << "Access Granted" << endl;
	 retry = -1;
	}
	else if(digits!=check)
	{ 
	 retry--;
	 cout << "Not Matching." << endl;
	 if(retry)
		cout <<"Try Again" << endl;
	}
 }
 if(retry == 0)
	cout << "Access denied!!" << endl << "Bye" << endl;
 return 0;
}
Last edited on
Topic archived. No new replies allowed.