How would I get my program to compare user imput to text stored in a file? Like checking to see if the user entered the correct password.
Thanks for the help.
How would I get my program to compare user imput to text stored in a file? Like checking to see if the user entered the correct password.
Thanks for the help.
Open the file, read the password, close the file, read input from user and strcmp() it.
Read the password from the file, store it in a string and then compare it to the user input.
No matter what I enter in for the password it prints out that i have entered it in wrong, What did I do wrong, here is my code.
Code:#include <fstream.h> #include <iostream.h> int main() { int end; char password[21]; char realpass[21]; cout<<"Password:\n"; cin.get(password , 20); ifstream a_file ( "password.txt" ); a_file>> realpass; if (password != realpass) { cout<<"You have wrongly entered the password \n"; } else { cout<< "you have correctly entered the password\n"; } cin >> end; return 0; }
Use strcmp(), and you don't need the curly things for only one statement.
Example:EDIT: Added code tags.Code:if( strcmp(bleh, blah)) cout << "oh my word"; else cout << "more word action";
i used strcmp but it still says i have entered in the wrong code no matter what i put in
Code:#include <fstream.h> #include <iostream.h> int main() { int end; char password[21]; char realpass[21]; cout<<"Password:\n"; cin.get(password , 20); ifstream a_file ( "password.txt" ); a_file>> realpass; if (strcmp(password , realpass)) cout<<"You have wrongly entered the password \n"; else cout<< "you have correctly entered the password\n"; cin >> end; return 0; }
Since it's C++, remove the .h on your headers. You'd probably want to be using namespace std too.
I did mention strcmp() in my last post but I was too lazy to edit it so you should use strings instead of character arrays. #include <string> and change cin.get(password, 20) to getline(cin, password), with password being a string instead of char[21].Code:#include <fstream> #include <iostream> using namespace std;
After that it should be fine, even the != doesn't need to be changed.
[edit]
"i used strcmp but it still says i have entered in the wrong code no matter what i put in"
Input strings gets the newline character written to them if they fit. So if a user types "somepass" it gets stored as "somepass\n". This could be your problem if the one read from the file doesn't end with a newline character.
You could try outputting the two strings with puts() and see if they actually differ, just remove them when you're done. It's possible that the string you read from the file isn't correct either.
Last edited by OnionKnight; 03-19-2006 at 11:05 AM.
ok i am confused, what is the difference in a string and a character array, sorry if it is a stupid question.
Simply put a string is a character array that changes it size when needed so you aren't restricted to a set like 21 in your program.
The string class is also the C++ way of handling text but it is of course still legal and ok to use character arrays if you want.
'string' is the C++ string type. Essentially a way to make your life much, much easier in C++.
Don't use char[] or strcmp unless you want to make life difficult (or if you're programming in C rather than C++)
eg,Much easier to read and understand!Code:#include <iostream> #include <string> using namespace std; int main() { const string password = "mypass"; string input; cin >> input; if (password == input) cout << "Password match"; }
how woul i get this to check and see if the password the user entered matched teh first line of the file named list.txt?Originally Posted by Bench82
Was i doing that right in my previous post?
Argggghhhh!!! (this to the other helpful posts)Originally Posted by laar
What you did wrong is not find out what was actually read from the file/user.
This is always the easiest way to find out what is wrong. It works most of the time:
After you read password, display it:
Surrounding the input with [ and ] lets you see of the newline is in the value or not.Code:cout << "[" << password << "]" << endl;
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
thank you, turns out i needed to put C://password.txt instead of password.txt but it works nowOriginally Posted by WaltP
thank you for your help