Entery code ..错误:entery.exe停止工作。 C语言

时间:2016-05-10 07:19:09

标签: c

此代码应输入用户名和密码..用户名为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;
 }

2 个答案:

答案 0 :(得分:2)

您的计划存在多个问题,其中包括:

  • username扩展为指向字符指针的指针数组
  • username的长度不足以保存默认密码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;
}

答案 1 :(得分:-1)

How to read / parse input in C? The FAQ.。从“不要将*scanf()用于可能格式错误的输入”部分开始,然后从那里继续阅读。

我不会给家庭作业问题提供现成的答案,但会对您的特定问题提供一些提示:

char* *username[5]

这是指向char指针的5个指针数组。不是你想要的,真的。你想要一个字符数组,a.k.a。一个“字符串”。

for (i=0;i<5;i++){
        scanf("%c", &username[i]);  
    }

这个(%c)一次读取一个字符。同样,您需要字符串。您可以使用scanf( "%s", ... )执行此操作,但您确实不应该这样做。你想要fgets()

if ((*username[5]=="Admin")&&(password==2016))

username[5]?你一次读一个字符后?你看到了问题吗?

您可能感兴趣的是一个名为strncmp的函数。