C++ password-protected entry into program
Hey,
I've just started programming in C++, and after reading (quite a lot) today, I managed to put together an extremely crude login for a terminal based application. My code is as follows: Code:
#include <iostream> Code:
jk9@ubuntu:~/CPP/000my_stuff/pass_prot2$ g++ passprot2.cpp Anyway, I know that this is a very crude way of password protection, and I'm guessing it's insecure(?), but I was wondering what other ways there are of password protecting something in C++ code. Thanks in advance, J_K9 P.S Is it possible to decompile an a.out file to view the original C++ source? |
password protection
I altered your code:
[CODE ]#include <iostream.h> #include <string.h> using namespace std; //An attempt at password protection //By J_K9 -- 10/07/2005 int main() { const string passwd("password"); string input_passwd; cout << "Please enter your password: "; cin >> input_passwd; if (input_passwd == passwd) cout << "\nPassword matches!\n"; else cout << "\nSorry! Wrong password!\n"; return 0; } [/CODE] Notice the change, I included the string class. If you do not use this class, you must use character arrays. Read up on it. Hope this helps. |
Okay now you've got me interested...:D I don't usually tend to use "using namespace std;", as I prefer to declare std::cout, std::cin etc...If I did it my way (using std::xxx) would the "string" values be std::string or not? (I don't think so but just checking).
I will definitely read up on string classes and character arrays (:O), but can I just quickly ask what that did, and why it works whereas mine didn't? In my book (SANS Teach Yourself C++ in 24 Hours) it didn't mention the "string" value, which I actually used to use a lot with BASIC variables. I thought that from the information provided in the book, the only values allowed for strings were "char" and "float", but obviously "string" also exists....Yet, why do you need to "#include string"? Does this mean that "string" is not a variable type? Please teach me what's going on :D! Thank you very much for your help so far! edit: Oh yeah, just remembered, is it possible to decompile an a.out file so that you can see the original source code? I don't think so but I just want to check, because if not that would really defeat the purpose of my code! ;) J_K9 P.S I've been told to buy Accelerated C++: Practical Programming by Example and read that instead of my current book.... |
why include <string>
When you include <string>, you are linking the sting class. The string class is a pre-defined class. You may also create user defined classes. This is using an object oriented approach to programming in C++. Read up on classes. They are used in many different languages.
|
All times are GMT -5. The time now is 04:11 PM. |