I working on part of a code for a much larger project. I understand how to write a program for a password in the simplest form, but I want to have it let the user guess the password up to 5 times and then not let them run a command.
For example if the user guess the password right then he can type debug and get the source code, but if he guess it wrong 5 times the app won't accept the command debug.
This code is in a rough state. What I'm having trouble is having it write out to the file. I know how to write out to a file, but this is driving me nuts. How the code is, it will keep on prompting for the password until 5 wrong guess or the correct password. So I wanted to have a way for the user can get out of that and do something else. So I coded an exit command, but it isn't working right for me. When they type exit I wanted to write the number of wrong guess so far to a file and then when they try again it will check the file and use that current number of guess for the count.
Any help or suggestion would be greatly appreciate.
Sincerely your;
jdm
P.S. If it isn't clear what I'm trying to do just ask.
Code:
#include <iostream>
#include <fstream>
using namespace std;
void debug();
int main()
{
//open a file to read and write
//declare file variables and name of file
ifstream inData;
ofstream outData;
inData.open("guess.txt");
outData.open("guess.txt");
string password;//store password input
int number = 0;//store number of guess
int num = 0;//store number of guess from file
inData >> num;//get number
//making sure number of guess isn't 5
if (num == 5)
{
cerr << "Access Denied" << endl;
system("pause");
exit (100);
}
//A while loop that will prompt for the password as long as the number of guess isn't 5 and the right password isn't entered in
while (number !=5 && password != "access")
{
cout << "Enter password: ";
cin >> password;
//trying to let the user exit the prompt, but keep track of the number of guesses, but it isn't writing out to the file right. I don't know why
if (password == "exit")
{
outData << number;
system("pause");//through in thinking that the app was closing to early to write to the file.
exit(100);
}
//launch the debug, but later it will allow the user the option of entering that command in.
else if (password == "access")
{
debug();
}
//increase number of guesses by 1 when wrong password is entered in
else {
number++;
}
outData << number << endl
cerr << number << endl;
}
//close files
inData.close();
outData.close();
system("pause");
return 0;
}
//just a test dummy
void debug()
{
cerr << "Debug" << endl;
cout << "Code Here" << endl;
}
Thanks for the help.
jdm