我正在用C做注册码,基本上可以输入多个账户. 现在,我想要的是通过登录函数访问该特定帐户,其中(Pass == LogIn Pass).

尽管我的代码已经可以访问该特定帐户(请参阅Inquiry()函数),但我的问题是,当我try 使用该特定帐户的密码确认LogIn_Pass时,即使用户输入正确,它也总是返回"帐户未找到".

我已经判断过Inquiry()是否有效,并且它基于LogIn()中输入的账号.

我的问题是LogIn_Pass无法与从Registry输入的密码进行比较(accounts[i][4]).

请参阅登录功能."欢迎"声明永远不会发表.我如何才能成功比较它们?

我是C新手,我被难住了.请帮忙.

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

char accounts[100][4][100]; 
int num_accounts = 0, confirm_pass, login_pass; 
int acc_num;
int login_pass;

// Function declarations
void Registration();
void Inquiry();
void LogIn();

//Main Function
int main() {
    int n;
    
    
    Registration();
    LogIn();
    
    while (1) {
        printf("\n1. Inquiry\n2. Exit\n");
        scanf("%d", &n);
        
        switch (n) {
            case 1: 
                Inquiry();
                break;
            case 2:
                printf("Exiting...\n");
                return 0;
            default:
                printf("Invalid choice!\n");
                break;
        }
    }
    return 0;
}

//Registration Function
void Registration() {
    printf("Enter the number of accounts to register: ");
    scanf("%d", &num_accounts);
    
    
    for (int i = 0; i < num_accounts; i++) {
        getchar();
        printf("\nRegistration for Account %d\n", i + 1);
        printf("____________________________\n");
        printf("1. Account name: ");
        fgets(accounts[i][0], sizeof(accounts[i][0]), stdin);
        printf("2. Account number: ");
        fgets(accounts[i][1], sizeof(accounts[i][1]), stdin);
        printf("3. Address: ");
        fgets(accounts[i][2], sizeof(accounts[i][2]), stdin);
        printf("4. Mobile number: ");
        fgets(accounts[i][3], sizeof(accounts[i][3]), stdin);
        
        // Prompt for password creation
        do {
            printf("\n\nCreate Password: ");
            fgets(accounts[i][4], sizeof(accounts[i][4]), stdin);
            printf("Confirm Password: ");
             fgets(accounts[i][5], sizeof(accounts[i][5]), stdin);
            
            if (strcmp(accounts[i][4], accounts[i][5]) == 0) {
                break;
            } else {
                printf("Password does not match!\n");
            }
        } while (1);
    }
}

//LogIn Function
void LogIn() {
    
   
    printf("\nLog In: \n");
    printf("____________________________\n");
    printf("Account number: ");
    scanf("%d", &acc_num);
    printf("Password: ");
    scanf("%d", &login_pass);
    
    // Find the account with the matching account number
    int found = 0;
    for (int i = 0; i < num_accounts; i++) {
         if (acc_num == atoi(accounts[i][1]) && login_pass == atoi(accounts[i][4])) {
            printf("\nWelcome!\n");
            found = 1;
            break;
        }
    }
    
    if (!found) {
        printf("\nAccount not found!\n");
    }
}

//Inquiry Function
void Inquiry() {
    for (int i = 0; i < num_accounts; i++) {
        if (acc_num == atoi(accounts[i][1])) {
             printf("Account name: %s", accounts[i][0]);
            printf("Account number: %s", accounts[i][1]);
            printf("Address: %s", accounts[i][2]);
            printf("Mobile number: %s", accounts[i][3]);
            break;
        }
    }
    
}

推荐答案

OP : "How can I successfully compare them?"

答案:不删除它们.

char accounts[100][4][100];

显示您打算记录4个字段的100条记录,每条记录长100个字符.
然后,创建帐户时:

fgets(accounts[i][4], sizeof(accounts[i][4]), stdin); and...
fgets(accounts[i][5], sizeof(accounts[i][5]), stdin);

正在将信息记录到imagined行5 6中(即:beyond the intended 4 rows of data).

当用户输入记录#2的第一个字段时,输入replaces记录#1的密码是什么.这是不幸的.


最低修复方法:使用enum个令牌来命名每条"记录"的"行",而不是"神奇数字"

enum { eName, eAcct, eAddr, eMobl, ePswd, nFlds };


NB:将这些编译器标记的定义放置在源代码的top处(#include行之后).


然后,类似于:

        printf("1. Account name: ");
        fgets(accounts[i][ eName ], sizeof(accounts[i][ eName ]), stdin);
        printf("2. Account number: ");
        fgets(accounts[i][ eAcct ], sizeof(accounts[i][ eAcct ]), stdin);
        printf("3. Address: ");
        fgets(accounts[i][ eAddr ], sizeof(accounts[i][ eAddr ]), stdin);
        printf("4. Mobile number: ");
        fgets(accounts[i][ eMobl ], sizeof(accounts[i][ eMobl ]), stdin);
        
        // Prompt for password creation
        do {
            printf("\n\nCreate Password: ");
            fgets(accounts[i][ ePswd ], sizeof(accounts[i][ ePswd ]), stdin);

            printf("Confirm Password: ");
            char tmp[ sizeof accounts[i][ ePswd ] ]; // 2nd entry into temp buffer

            fgets( tmp, sizeof tmp, stdin);
            
            if (strcmp( accounts[i][ ePswd ], tmp ) == 0)
                break;

            printf("Password does not match!\n");
        } while (1);

然后固定分配线:

char accounts[100][ nFlds ][100];


Then, go through entire code, replacing ALL the magic numbers with tokens assigned a definite value in only one location.

太多的"新手乐观主义",没有仔细关注每一个重要细节.
此处显示的代码尚未编译或测试.仅用于教学目的.


当热情的用户try 创建162个帐户时会发生什么?
您的程序处理得当吗?

学习是艰难的.坚持下go .

C++相关问答推荐

Linux/C:复制修剪了最后一个填零孔的文件

如何从TPS特定的TGPT_PUBLIC数据 struct 中以OpenSSL的EVP_PKEY

ARM上的Modulo Sim Aarch 64(NEON)

是否定义了数组指针类型转换为指针类型?""""

在32位处理器上优化53—32位模计算>

C语言中字符数组声明中的标准

C是否用0填充多维数组的其余部分?

Make Node函数.S有什么问题吗?

VS代码';S C/C++扩展称C23真关键字和假关键字未定义

插座打开MacOS组件

获取每个循环迭代结束时的当前时间

将字符串数组传递给C中的函数:`str[dim1][str_size]`vs`*str[dim1]`

如何在C++中安全地进行浮点运算

获取前2个连续1比特的索引的有效方法

覆盖读取函数,但当文件描述符为3或4时,我有问题

通过对一个大的Malloc内存进行切片来使用Malloc的内存片

如何在C中处理流水线中的a、n命令?

我可以使用Windows SDK';s IN6_IS_ADDR_LOOPBACK等,尽管没有文档?

#define X Defined(Y) 是有效的 C/C++ 宏定义吗?

OpenGL 中的非渐变 colored颜色 变化