Creating a simple password program in C++

In summary: Sometimes the error creep in because of a misplaced semicolon: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:for(int i=0; i<5; i++) { printf("Hello World!"); // the FOR loop runs 5 times and prints Hello World 5 times as expected...
  • #1
The_Inventor
9
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?
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:
  • Like
Likes Telemachus
Technology news on Phys.org
  • #3
Your loop is in the wrong place. Try this:
Code:
  ...
do
  cout << "Enter password: ";
  cin >> userinput;
  if (userinput != password)
    cout << "Password Invalid... Try Again: ";
  while (userinput != password);
cout << "Password Accepted... ";
  ...
 
  • #4
The_Inventor said:
while (userinput != password)
cout << "Password Invalid... Try Again" << endl;
cin >> userinput;
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.)
 
  • Like
Likes The_Inventor
  • #5
haruspex said:
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.)
Wow, such a simple mistake. It works now, thanks a lot!
 
  • #6
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.
 
  • #7
jedishrfu said:
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.

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.
 
  • #8
The_Inventor said:
Is there a way to do this in C++?
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.
 
  • #9
The_Inventor said:
Wow, such a simple mistake. It works now, thanks a lot!

Sometimes the error creep in because of a misplaced semicolon:

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:

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.
 
  • Like
Likes jim mcnamara
  • #10
haruspex said:
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.

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.
 
  • #11
@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.
 
  • #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
 
  • #13
jim mcnamara said:
I view the OP as purely a learner and assume nothing important will be behind the authentication.
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:
 
  • #14
jim mcnamara said:
@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.

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.
 
  • #15
I have not got a lot to hide., it makes stuff too complicated.
 

1. What is a password program in C++?

A password program in C++ is a software application that allows users to create and manage passwords for their accounts and devices. It typically includes features such as generating secure passwords, storing them in an encrypted format, and allowing for easy retrieval and management of passwords.

2. How do I create a simple password program in C++?

To create a simple password program in C++, you will need to have a basic understanding of the C++ programming language and its syntax. You will also need to use libraries or functions that allow for user input and encryption. It is recommended to follow a tutorial or use a code template to help you get started.

3. What are the essential components of a password program in C++?

The essential components of a password program in C++ include a user interface for inputting and managing passwords, a password generation function, a storage system for encrypted passwords, and error handling to ensure the security and functionality of the program.

4. How can I make my password program in C++ more secure?

To make your password program in C++ more secure, you can implement encryption algorithms such as SHA or AES to encrypt the passwords. You can also add additional security measures, such as two-factor authentication or biometric authentication, for added protection.

5. Are there any potential drawbacks to creating a password program in C++?

As with any software, there are potential drawbacks to creating a password program in C++. These may include vulnerabilities in the code that could be exploited by hackers, user error in managing passwords, and the responsibility of maintaining and updating the program to ensure security. It is essential to regularly review and update the program to address any potential issues.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
978
Back
Top