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];
}
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;
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()).
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;
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.