c - 使用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);
}
解决方案
#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);
}
}
推荐阅读
- gmail - Gmail 正文模板
- python - 将字符串列表转换为字典,其中每个字符串的前 4 个字母是键,其余的是 Python 中的值
- blazor - 主页 blazor 的不同导航菜单仅在第二次单击后才起作用?
- azure - 如何在 Azure 数据工厂中为 Blob 接收器定义翻转文件大小?
- angular - 如何在同一个 NGINX 上为 Angular 和 Laravel 文件夹运行 certbot
- visual-studio-code - 我可以安装一个 vscode 服务器而不是每个用户一个吗?
- templates - 如何使用 ComponetEditor 或 PropertyEditor 创建/注册组件模板
- wordpress - WordPress 仅将帖子保存为新修订
- ios - 圆角 Swiftui 从视图中的较高位置开始
- spring-data-jdbc - 为什么我不能使用 Spring Data Jdbc 进行引用查询?