Password

This is a discussion on Password within the C++ Programming forums, part of the General Programming Boards category; >Sorry, that's the best I can do as a newbie programmer. No biggie, just don't take the following review the ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Sorry, that's the best I can do as a newbie programmer.
    No biggie, just don't take the following review the wrong way.

    >float a_a, b_b;
    It's best to choose names that are meaningful to other people and yourself in the future. It saves you all kinds of heartache later on. Also, for multiple reasons but mostly because double is the default floating-point type, you should always use double unless you have a compelling reason not to.

    >b_b = 123
    Don't forget the trailing semicolon.

    >if ( a_a == b_b ) {
    I highly recommend not using floating-point data types until you know how to use them correctly. This isn't a flame or anything, it's just that floating-point is incredibly subtle and difficult to work with for just about everyone. If you aren't familiar with the pitfalls then you'll have weird problems that you won't know how to deal with.

    In this case, comparing floating-point values for equality is inaccurate because the binary representation of floating-point is inherently imprecise. If you have a_a=1.1 and b_b=1.1 then they still might not be equal if a_a is 1.100001 and b_b is 1.100000 internally.

    The solution is to do a "close enough" test that would be dreadfully confusing at this point in your studies, which brings me back to the initial recommendation.

    >else if ( a_a != b_b ) {
    else nothing is equivalent to else a_a != b_b.

    >cin.get();
    Good idea, but because you do this for every path taken, you can place it outside of the if statement.

    Ignoring the floating-point issues, here's your updated code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      double pass, input;
    
      pass = 123.0;
      cout<<"Please enter your password: ";
      cin>> input;
      cin.ignore();
    
      if (input == pass)
        cout<<"Correct!"<<endl;
      else
        cout<<"Incorrect"<<endl;
    
      cin.get();
    }
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Yeah...

    Yeah...Could you explain the whole doublepass thing. Also, you may beleive the whole thing is ugly, but if you take out the spaces(which are put there automatically), it looks a lot neater. Thanks for the cin.get(); outside the if statement suggestion, it helps me learn how c++ works. How could I take this the wrong way? I'm happy that after 2 days I can offer an answer to a question answered by someone else! And yes, it may not be too pretty, but I am still happy that the syntax works!

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Could you explain the whole doublepass thing.
    pass and input are just more informative names than b_b and a_a, respectively.

    >How could I take this the wrong way?
    You would be surprised at the ways my posts have been misunderstood in the past.
    My best code is written with the delete key.

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    so what does the command 'double' do?

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so what does the command 'double' do?
    Basically, the same thing as float. The only difference is that double might be able to hold bigger or more precise values.
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    Ok...The coding is simpler, but the main obstacle to get over now is that the password can still only be numbers...Got any ideas?

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You want string data. The easiest way to do what you want in C++ is to use the string class, and getline:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string pass, input;
    
      pass = "x04G9Ytm";
      cout<<"Enter your password: ";
      getline(cin, input);
    
      if (input == pass)
        cout<<"Correct!"<<endl;
      else
        cout<<"Incorrect"<<endl;
    
      cin.get();
    }
    Compare this program with the one I posted earlier. Spot the differences and try to figure out the purpose of each of them.
    My best code is written with the delete key.

  8. #23
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Thantos:
    MD5 is a hash scheme, not an encryption scheme. There is a very important difference between the two.

  9. #24
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes but you can use it to encrypt a password since only one way is needed.

  10. #25
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Isee what you mean now.

  11. #26
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Smile Strings....

    That's what I thought would be needed. However, I am still learning basic concepts. After trying to figure out the whole string situation for half an hour, I decided to give up and ask for someone else's input. Thanks for the code, it helps explain concepts of c++. Very valuable.

  12. #27
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    If you're really looking for an MD5 scheme, then here would be a good reference:
    http://www.codeguru.com/code/legacy/...ms/md5_src.zip

    If you don't have visual C++, you can still open the source files in notepad and go from there. You might not want to start out with this if you're a beginning coder and are just learning still, but regaurdless this will do what you want to do if you read/modify the code correctly

    If you're just trying to get past notepad readers, then just implement a 1-1 crypto scheme, it wouldn't be that hard really, say your password is a char array of size 11 including the null terminator, here's what it would look like:
    Code:
    #include <iostream>
    
    int main(int argc, char **argv)
    {
    /* Program: 1-1crypto.cpp
        Author: Tronic
        Description: Detailing a simple 1-1 crypto algo.
        Compiled In: Visual C++ 7
    /*
    bool access = false;/*mainly use this if you want to change/expand this code*/
    int successCounter = 0;
    	char matchCipher[11] = "abcdefghij";
    	for(int i = 0; i < 10; i++)
    		matchCipher[i]++;
    	char plainCipher[11] = { 0 };
    	std::cout << "> ";
    	  std::cin >> plainCipher;
    	for(int i = 0; i < 10; i++)
    		plainCipher[i]++;
    	for(int i = 0; i < 10; i++)
    	{
    		if(!(plainCipher[i] == matchCipher[i]))
    		{
    		  access = false; //Just in case something goes wacko.
    		  std::cout << "Access Denied";
    		}
    		else
    			successCounter++;
    	}
    	if(successCounter == 10)
    	{
    		bool access = true;//Might need this for later use.
    		std::cout << "Access Granted";
    	}
    return 0;
    }
    -The main holes in this are successCounter, buffer overflow when getting input for plainCipher because of no bounds checking, and the simple scheme.

    Prelude, what makes != more precise with floating points than == ? I haven't ran into this problem yet lol, good that I find out about it now from someone.
    Last edited by Tronic; 12-17-2004 at 11:13 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #28
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>what makes != more precise with floating points than ==
    I don't think it is. Otherwise, you could just do:
    if(!(x != y))
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #29
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Well, from what Prelude says it is, but that confuses me since I don't exactly know the steps behind != and ==. I would assume that == and != have a different precisity, but that's just an assumption.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  15. #30
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by Tronic
    Well, from what Prelude says it is
    It has nothing to do with != being more accurate than ==, what Prelude was addressing was the fact that a floating point number is inherently inaccurate, more of a "guess". A double is more accurate of a guess than a float, but neither is 100% exact.

    For either of them, the computer may store the number 1.1 as 1.10001, or it may store 1.1 as 1.10000. It may even store two float variables defined at the same time with these two different values. So, while you print out 1.1000 for both numbers, because of the possible inaccuracy, testing for equality of those two floats may turn out false (because 1.10001 is NOT equal to 1.10000).

    This means that testing for equality ( == ) is most likely to be false for any two given float variables, while testing for inequality ( != ) is most likely to be true for any two given float variables.

    I have played with the way floating point variables are stored internally with gcc and it's fascinating (in a morbidly geeky sort of way).
    Last edited by jEssYcAt; 12-18-2004 at 12:07 AM.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 06:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21