LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ password-protected entry into program (https://www.linuxquestions.org/questions/programming-9/c-password-protected-entry-into-program-341996/)

J_K9 07-10-2005 08:14 PM

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>

//An attempt at password protection
//By J_K9  -- 10/07/2005

int main()
{
        const int passwd = 13579;
        int input_passwd;
        std::cout << "Please enter your password: ";
        std::cin >> input_passwd;
        if (input_passwd == passwd)
                std::cout << "\nEnter!\n";
        else
                std::cout << "\nSorry! Wrong password!\n";
        return 0;
}

I have compiled this and I know it works, but I had an odd complaint from g++ when I tried replacing the "int" values with "char", and changing the passwd to "lycrolite" (including speech marks). The error I received was:
Code:

jk9@ubuntu:~/CPP/000my_stuff/pass_prot2$ g++ passprot2.cpp
passprot2.cpp: In function `int main()':
passprot2.cpp:8: error: invalid conversion from `const char*' to `char'

I don't really understand this error...invalid conversion? I declared my constant and my variable, only initialized my constant, but the compiler is telling me that there is an invalid conversion?

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?

dugas 07-10-2005 09:30 PM

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.

J_K9 07-10-2005 09:52 PM

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....

dugas 07-12-2005 01:11 AM

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.