DEMONTH 2021-11-06 23:03 采纳率: 100%
浏览 5
已结题

关于写注册登录时所遇到的问题

当我选择注册时,会跳过输入用户名这句代码进行下一步;同时也希望xdm能指出其他错误!

#include <stdio.h>
#include <string.h>
#include <windows.h>
#define N 20

void land();
void login();
char *s_gets(char *st, int n);

typedef struct User
{
    char name[N];
    char password[N];
    struct User *next = NULL;
} user;

user *head = NULL;
char name[N] = {'\0'};
char password[N] = {'\0'};

int main(void)
{
    printf("输入1为登录,输入2为注册:\n");
    int n;
    scanf("%d", &n);
    switch (n)
    {
    case 1:
        land();
        break;
    case 2:
        login();
        break;
    default:
        break;
    }
    system("pause");
    return 0;
}

void land()
{
    user *phead = (user *)malloc(sizeof(user));
    phead = head;
    printf("请输入你的用户名:\n");
    scanf("%s", name);
    while (phead->next != NULL)
    {
        if (strcmp(name, phead->name) == 0)
        {
            printf("请输入你的密码:\n");
            scanf("%s", password);
            if (strcmp(password, phead->password) == 0)
                printf("登录成功!");
            else
            {
                printf("登录失败!");
                break;
            }
        }
        else
        {
            printf("不存在此用户名!");
            break;
        }
    }
}

void login()
{
    user *phead = head;
    while (1)
    {
        if (phead == NULL)
        {
            char ch;
            ch = getchar();
            printf("请输入你的用户名:\n");
            s_gets(head->name, N);
            printf("请输入你的密码:\n");
            s_gets(head->password, N);
            break;
        }
        else
        {
            phead = phead->next;
        }
    }
}

char *s_gets(char *st, int n)
{
    char *ret_val;
    char *find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
            *find = '\0';
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月14日
    • 创建了问题 11月6日