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;
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.
#include <iostream>
#include <fstream>
usingnamespace std;
int mySequentialSearch(constint digitCode);
int main() {
constint 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;
}
elseif(countAttempts == MAX_ATTEMPT)
{
cout << "Acess Denied\n";
found = true;
}
elseif(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(constint 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! :)
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.
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
}