首页 > 解决方案 > 使用C语言的简单登录程序

问题描述

所以,我正在做一个额外的实验室任务,需要我构建一个简单的登录程序来使用 C 读取学生 ID 和密码。我尝试使用 strcmp() 来验证 ID 和密码,但即使比较的元素是正确的,程序将始终显示“密码错误”。请帮我看看我的程序,任何帮助表示赞赏。提前致谢

以下是我的编码:

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
    char studentID[]="Clarance", password[]="123456", id[8], p[6];
    int n=1, x, y;

    do{
         printf("\nStudent_ID:");
         scanf("%s", &id);

         printf("\nPassword:");
         scanf("%s", &p);

         x=strcmp(id, studentID);
         y=strcmp(p, password);

         if(x==0 && y==0){
           printf("\nSucessfully Logged In");
         }else {
           printf("\nWrong Password, try again", 5-n);
            getch();
            n++;}

         if(n>5){
          printf("\nAccess Denied");
          getch();
          }

       }while (n<=5);

}

标签: c

解决方案


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char password [1000];
    printf("Hi there, please enter your password to verify if you are Samarth Matam: ");
    gets(password);
    if(strcmp(password, "Password")==0)
    {
        printf("Okay! You are Samarth Matam, hence you are allowed!");
    }
    else
    {
        printf("Wrong password!");
        exit(0);
    }
}

推荐阅读