Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 密码提示_C_Password Protection - Fatal编程技术网

C 密码提示

C 密码提示,c,password-protection,C,Password Protection,我正在尝试编写一个简单的密码提示。。我必须承认,我发现了一些代码,但它不适合我的特定用途。我还在学习C语言,这只是向上爬的又一步。如果密码正确,我试图让它继续运行,如果密码不正确,它将终止 int c; char pass[20] = ""; char *end = pass + sizeof(pass) - 1; char *dst = pass; printf("Enter password: "); while ((c = getchar()) != EOF && c !

我正在尝试编写一个简单的密码提示。。我必须承认,我发现了一些代码,但它不适合我的特定用途。我还在学习C语言,这只是向上爬的又一步。如果密码正确,我试图让它继续运行,如果密码不正确,它将终止

int c;
char pass[20] = "";
char *end = pass + sizeof(pass) - 1;
char *dst = pass;

printf("Enter password: ");
while ((c = getchar()) != EOF && c != '\n' && dst < end)
*dst++ = c;
*dst = '\0';  // Ensure null termination

printf("\nPass: %s\n", pass);

return 0;
intc;
字符传递[20]=“”;
char*end=pass+sizeof(pass)-1;
char*dst=通过;
printf(“输入密码:”);
而((c=getchar())!=EOF&&c!='\n'&&dst

在“输入密码”提示下,代码似乎“锁定”。谢谢你的帮助。非常感谢。

您需要使用
strcmp
添加
if
语句,以检查输入的密码是否正确

#include<stdio.h>
#include <stdlib.h>

int main(void)
{
    int c;
    char pass[20] = "";
    char *end = pass + sizeof(pass) - 1;
    char *dst = pass;
    char admin[] = "12345";

    printf("Enter password: ");
    while ((c = getchar()) != EOF && c != '\n' && dst < end)
        *dst++ = c;

    *dst = '\0';  // Ensure null termination

    if(strcmp(admin, pass) == 0)
        printf("\nPass: %s\n", pass);
    else
    {
        printf("Incorrect password");
        return 0;
    }

    return 0;
}
#包括
#包括
内部主(空)
{
INTC;
字符传递[20]=“”;
char*end=pass+sizeof(pass)-1;
char*dst=通过;
字符管理[]=“12345”;
printf(“输入密码:”);
而((c=getchar())!=EOF&&c!='\n'&&dst
代码似乎在这里正确地打印了通行证。。。你说的“锁起来”是什么意思?就是这样。它不接受输入。在打印“输入密码:”后,
getchar
将等待您输入内容。如果执行此操作并按enter键,会发生什么情况?还是“锁着”吗?程序是否终止?您的意思是希望它在用户键入密码时隐藏密码?比如删除他当前键入的带有星号的pw字符?@user2990606:很高兴听到你找到了解决方案。但请考虑下次注意你的问题描述。通过说代码“锁定”,你误导了相当多的人,暗示你无法从控制台程序中得到任何响应,可能是因为一个无限循环,而你实际上只是在寻找一种比较字符串的方法;密码输入is.OP说,对他来说,输入锁是其他人(包括我)无法在他们的机器上复制的。您的更正与程序的逻辑有关。我认为OP还没有实现它,因为输入问题。我和其他评论者也没有,见上文。目前尚不清楚OP的真正问题是什么;我们将不得不等待更多的信息。可能是
stdin
的系统依赖行为,以及未冲洗的'stdout',谁知道呢?恭喜!看起来“年度透视者”的称号已经在元旦颁发了。我从来没有想到“锁定”真的意味着缺少字符串比较。没有冒犯的意思。当然,海报上是这么问的,但最后一段关于“锁定”的内容让我完全走错了方向。而且,从其他评论来看,不仅仅是我。不要介意。在一个非常简单的问题上浪费了足够多的时间。