首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中创建密码

在C++中创建密码
EN

Stack Overflow用户
提问于 2013-07-11 05:37:23
回答 1查看 4.1K关注 0票数 0

我希望帮助我的一个技术营员创建一个程序,允许他们检查密码是否输入正确。我对此了解有限,如果您能提供任何帮助,我们将不胜感激。谢谢。

代码语言:javascript
复制
//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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-11 06:00:35

我可以看到你的代码中有很多问题。首先,您可能希望do~while循环也包含密码检查条件,否则只在用户输入空行后才检查密码(只有当您的密码确实是空行时,才会匹配密码)。cin.getline()提示用户输入字符串,因此只有在用户键入字符串后,才会执行下一行。

不能使用'==‘运算符在C++中进行字符串比较,这不会产生预期的效果。本例中的'==‘操作符将对字符串的第一个字母执行ascii字符代码检查。如果要进行字符串比较,则需要使用诸如strcmp()这样的比较函数,如果没有差异,则返回0。

您也没有为string变量分配任何内存,这在C++中是一个很大的禁忌。许多脚本语言不需要这样做,但是C++中的字符串必须提前分配到您想要的大小,然后才能使用。

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17581329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档