Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Creating a simple password program in C++

  1. May 19, 2017 #1
    So I'm trying to teach myself C++ programming and I'm having trouble writing a password program.
    Essentially I want to program to ask the user to input a password, and then compare that input with the correct password. If the user input matches the password I want the program to output a message saying "password accepted", if they do not match then I want the program to output a message saying "password invalid... Try again:" and from there the user continues to try a password until they input the correct password. This seemed simple at first however the problem arises when I try to introduce a while loop. If the password is incorrect after the second try they program continues to loop the message "password invalid...Try again:" without giving the user a chance to type in the password. Below is my source code, I'm using visual studio by the way. Can anyone help me out?
    Code (C):

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <math.h>

    using namespace std;

    string Admission()
    {
        string userinput;                    // this is the user's guess
        string password = "TonyStark";        // this is the actual password
        cout << "Enter password: ";
        cin >> userinput;
        if (userinput != password)
        {
            cout << "Password Invalid... Try Again: ";
            cin >> userinput;
            while (userinput != password)
                cout << "Password Invalid... Try Again" << endl;
                cin >> userinput;
        }
        else
            cout << "Password Accepted... ";
     
        return "Welcome";
    }

    int main()
    {
        cout << Admission() << endl;

        system("pause>nul");
        return 0;
    }
     
    Last edited by a moderator: May 31, 2017
  2. jcsd
  3. May 19, 2017 #2

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

  4. May 20, 2017 #3

    Svein

    User Avatar
    Science Advisor

    Your loop is in the wrong place. Try this:
    Code (C):

      ...
    do
      cout << "Enter password: ";
      cin >> userinput;
      if (userinput != password)
        cout << "Password Invalid... Try Again: ";
      while (userinput != password);
    cout << "Password Accepted... ";
      ...
     
     
  5. May 20, 2017 #4

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    I see the problem now. The third statement above is not in the loop. You need {} braces.
    (It would have been obvious with the right indentation.)
     
  6. May 20, 2017 #5
    Wow, such a simple mistake. It works now, thanks a lot!
     
  7. May 20, 2017 #6

    jedishrfu

    Staff: Mentor

    Also real password algorithms use hashing so that the password is always hidden. They hash the user input and compare it to the hashed password with matching hashes meaning passwords match.
     
  8. May 31, 2017 #7
    Interesting, Is there a way to do this in C++? I've been learning this stuff on my own so there's many C++ commands, and tricks that I haven't been exposed to yet.
     
  9. May 31, 2017 #8

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Of course, but some hashing algorithms are better than others. First, you have to decide what level of risk you will tolerate for an accidental match. Say you have a good algorithm with a 32 bit hash. Then the risk of an accidental match is one in 232.
    CRC32 is very good as an algorithm; there is a choice of polynomials.
     
  10. May 31, 2017 #9

    jedishrfu

    Staff: Mentor

    Sometimes the error creep in because of a misplaced semicolon:

    Code (C):

    for(int i=0; i<5; i++) ;       // the FOR loop runs 5 times executing the  empty semicolon statement
    {
        printf("Hello World!");  // finally after the FOR loop completes then the print block prints Hello World
    }
     
    because I've done this in the past, I use the following brace formatting in my code:

    Code (C):

    for(int i=0; i<5; i++)  {
        printf("Hello World!");     // the FOR loop runs 5 times and prints Hello World 5 times as expected...
    }
     
    because seeing "; {" reminds me of a sad face ie it looks wrong when you see it.
     
  11. Jul 22, 2017 #10

    SixNein

    User Avatar
    Gold Member

    In real world programming, you want to use an algorithm like SHA512, and you also want to have a random salt generated. You also need to set a minimum complexity required of the password itself.

    The main factor in selecting an algorithm isn't accidental matches per say; instead, it is how fast someone can crack it using say rainbow tables. These days it's good practice to have a secondary means of authentication, i.e., two factor.
     
  12. Jul 23, 2017 #11

    jim mcnamara

    User Avatar

    Staff: Mentor

    @SixNein - it is really common for inexperienced (and some who know better ) programmers to come up with methods or algorithms for tasks they have no good understanding (about). You know - The ones best handled by the libraries in the OS/language implementation - Passwords, authentication in general, date/time manipulations, hashing, etc.

    I view the OP as purely a learner and assume nothing important will be behind the authentication. Maybe it is a way to block little brother/kid out of the system.
     
  13. Jul 25, 2017 #12
    Don't forget to "salt" your hashes!

    Hackers have rainbow tables that have the hashes for most well used passwords - if you salt the users password before performing the hash, then store the salt and the hash you'll be much safer and it doesn't add too much overhead
     
  14. Jul 25, 2017 #13

    jtbell

    User Avatar

    Staff: Mentor

    Indeed, this reads like something that I might have given as a simple exercise during the first few weeks of a programming course, after covering the basics of console I/O, if-statements and loops. I would of course have told students that real password algorithms also involve encryption and are more sophisticated than this.

    Crawling before walking before running before marathons. :oldwink:
     
  15. Jul 25, 2017 #14

    SixNein

    User Avatar
    Gold Member

    My response was mostly geared towards another poster of the thread although the OP may benefit from being aware of the existence of these ideas.
     
  16. Aug 1, 2017 #15
    I have not got a lot to hide., it makes stuff too complicated.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Creating a simple password program in C++
Loading...