Hello dear programmers,
well, I'm a beginner. I made a password for my program with a simple if condition, but I can't add the codes that let the user have the access to change it.
codes:
if (m == TRUE){
//my program
}
I added switch cases and one of them lets the user to edit his program, and one of the settings is to let him change his pass but I don't know how to add it, thanks.
Its quite easy actually. I am guessing you have your password as a string correct? If not it doesn't really matter.
All you need to do is this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Lets say this holds your password variable
string password = "hello";
// The switch case
switch (input)
{
// Lets say this is the one that changes the password
case 1:
cout << "Please enter your new password: ";
cin >> password; // Changes the password to what is entered
// You can put whatever else you need here to store your password in a text
// file or however you do it so that you can check the password against whatever
// The user enters.
break;
case 2:
// do whatever
}
I am guessing the password you have is hardcoded into the program and you are having trouble with letting the user change it and having the new password be saved after the program exits. The easiest way to do this would be to probably store the password in a text file or something. Then you can just check whatever the user enters as the password against what the password is in the text file to see if they entered the correct password. You can learn how to deal with fileIO at the tutorial here -> http://www.cplusplus.com/doc/tutorial/files/
Also for your if statement you only need to do this if (m) which is the same as if (m == TRUE).
I made what you did, nothing works. tried to add a few extra things on it, still nothing works. can you show me a small program please? if you have time I mean :D
EDIT: So I can copy/past it and learn from what you did.
There's multiple ways to do it of course, but here is what I wrote out really quick to show you the basic logic. Of course for a practical program there's more to it than this. If you need more explained let me know.
That will only allow there to be a password determined by the programmer only and the user wont be able to change the password and have it keep the new password (everytime you open the program it will be admin). The easiest way to have it so the user can set their own password would be to have the password save to a text file and then cross reference that text file whenever you want to require a password for that portion of your program.
And no I wont paste the code here so you can just copy and paste it because you wont learn that way ; p. Coding is not copy and paste it invovles doing research to solve the problems you have. So check out the link I gave for the file input and output and that should give you a good start to your research.
That will only allow there to be a password determined by the programmer only and the user wont be able to change the password and have it keep the new password (everytime you open the program it will be admin). The easiest way to have it so the user can set their own password would be to have the password save to a text file and then cross reference that text file whenever you want to require a password for that portion of your program.
Yes I'm aware as I said there's more to it than that. Like you though I don't want to just write out everything for him.