简体   繁体   中英

Creating a Password in C++

I am looking to help one of my tech campers create a program that allows them to check if a password is entered correctly. I have limited knowledge about this and we would very much appreciate any help you could provide. Thank you.

//This lets the user input a password to enter an area

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

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Please enter the password";
     char* userData;
    do 
    {
        cin.getline(userData, 70);
            //says the data back
        cout << userData;
    }while (userData != '\n');

    if (userData == 'This is my password')
    {
        cout << "Welcome back";
    }
    else 
    {
        while(userData != 'This is my password');
        cout << "******** INTRUDER ALERT *********";
    }

    return 0;
}

I can see a number of issues with your code. First, you probably want your do~while loop to include the password check condition as well, otherwise you are only checking the password after the user types in a blank line (which would only match the password if your password was literally a blank line). cin.getline() prompts the user for a string, so the next line will only be executed after the user types in a string.

String comparisons in C++ cannot be done using the '==' operator, this does not have the effect you intend. the '==' operator in your case will literally perform an ascii character code check on the first letter of the strings and only that. If you want to do a string comparison, you need to use a compare function such as strcmp() which returns 0 if there are no differences.

You are also not allocating any memory to use for your string variable, which is a big no-no in C++. Many scripting languages don't require this, but strings in C++ must be allocated ahead of time to a size you desire before they can be used.

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

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Please enter the password";
    char userData[71];   // I've allocated 71 characters to this string, which matches the amount of characters you accept in your getline call (plus one more for the required end of string character '\0').
    do 
    {
        cin.getline(userData, 70);
            //says the data back
        cout << userData;

        // Notice, I've moved this block inside the loop, as it needs to be tested after each password entry rather than after the user has input an empty line.
        if (!strcmp(userData, "This is my password")) // I've changed this statement to use the strcmp function to actually compare the values of the entire strings together.  The result of this function is the total number of differences found, so if it returns 0 that means the strings are the same.  Also note, I am using double quotes instead of single for the password value.
        {
            cout << "Welcome back";
            break; // I've added a break here to break out of the loop when the user inputs the correct password.
        }
        else 
        {
            // Removed the infinite loop, as it has no purpose other than locking up the program.
            cout << "******** INTRUDER ALERT *********";
        }

    }while (strlen(userData) != 0); // I'm not too sure about this, but I think if you enter a blank line it does not contain the '\n' within itself.  Instead, I've opted to use the strlen() function which tells you how many letters are in the given string.

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM