gpt4 book ai didi

c - 输入代码..错误:entery. exe停止工作。 C语言

转载 作者:行者123 更新时间:2023-11-30 20:23:05 26 4
gpt4 key购买 nike

此代码应该获取输入的用户名和密码。用户名是 Admin,密码是 2016。如果用户输入正确,它将打印登录过程已成功完成,否则它将要求用户输入他们再次..我编写了代码,但它不起作用,我不知道为什么..这里是:

#include <stdio.h>
int main(){
char* *username[5]; int password,choice; char Admin,i;

printf("Welcome to the Students' Registration System\n");
printf("Dear Registry employee: Kindly insert your username:\n");
for (i=0;i<5;i++){
scanf("%c", &username[i]);
}
printf("Insert your password:\n");
scanf("%d", &password);


if ((*username[5]=="Admin")&&(password==2016))
printf("The login process is successfully done");
else
while ((*username[5]!="Admin")||(password!=2016))
{
printf("The login process failed\n");
printf("Dear Registry employee: Kindly insert the correct username and password\n");
for (i=0;i<5;i++){
scanf("%c", &username[i]);
}
scanf("%d", &password);
}

printf("Please choose the number of your next step:\n");
printf("[1]Add new student\n");
printf("[2]Add new course\n");
printf("[3]Assign\remove courses for the student\n");
printf("[4]Search and view students' details:\n");
printf("[5]Request reports:\n");
printf("[6]Update student/course record:\n");
printf("[7]Delete student/course record:\n");

return 0;
}

最佳答案

您的程序存在多个问题,其中包括:

  • 用户名声明为指向字符指针的指针数组
  • 用户名的长度不足以保存默认密码admin
  • 使用循环读取用户名。
  • 使用 ==!= 运算符比较字符串。

更好的方法如下。

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

int main()
{
//Admin has 5 characters, and string requires one null terminator. So minimum length should be 6
char username[10];
int password,choice;
char Admin,i;

printf("Welcome to the Students' Registration System\n");


do
{
printf("Dear Registry employee: Kindly insert your username:\n");
//Use %s to read a string completely(till white space character)
scanf("%s", username);
printf("Insert your password:\n");
scanf("%d", &password);


//You can't compare string using == or !=
}while (strcmp(username, "admin") != 0 && password != 2016 );

printf("The login process is successfully done");

printf("Please choose the number of your next step:\n");
printf("[1]Add new student\n");
printf("[2]Add new course\n");
printf("[3]Assign\remove courses for the student\n");
printf("[4]Search and view students' details:\n");
printf("[5]Request reports:\n");
printf("[6]Update student/course record:\n");
printf("[7]Delete student/course record:\n");

return 0;
}

关于c - 输入代码..错误:entery. exe停止工作。 C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37131743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com