【问题标题】:How to compare between struct variables' values in c programming如何在c编程中比较结构变量的值
【发布时间】:2021-09-17 18:39:12
【问题描述】:

我正在尝试制作一个简单的程序,要求用户在注册时输入姓名、电子邮件和密码,然后让他再次输入电子邮件和密码以登录。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
        struct NewUserinfo{
        int age;
        char name[30], email[50], pass[16];
    }; 


    char e1[100], p1[16], e2[100], p2[16], name1[30], userprompt;
    printf("Welcome! Would you like to create an account? (y/n) ");
    CheckPrompt:
    scanf(" %c", &userprompt);
    switch(userprompt){
    case 'y':
        goto newUser;
        break;
    case 'n':
        goto caseN;
        break;
    default:
        printf("Invalid option. Please press \"y\" or \"n\".\n");
        goto CheckPrompt;
        break;
    }

    caseN:
        printf("Do you already have an account? (y/n) ");
        scanf(" %c", &userprompt);
         switch(userprompt){
    case 'y':
        goto alreadyUser;
        break;
    case 'n':
        goto end;
        break;
    default:
        printf("Invalid option. Please press \"y\" or \"n\".\n");
        goto caseN;
        break;
         }




    newUser:
        printf("Hello there. ");

        struct NewUserinfo user1;
            printf("What's your name? ");
            scanf("%s", user1.name);
            strcpy(name1, user1.name);
            printf("Your name is %s. ", user1.name);
            printf("What's your email? ");
            scanf("%s", e1);
            strcpy( user1.email, e1);
            printf("Your email is %s. What's your password? ", e1);
            scanf("%s", p1);
            strcpy(user1.pass, p1);
            printf("Your password is %s. \n", p1);
            printf("Thank you for signing up! Try our log in feature now.\n");

    alreadyUser:
        printf("Welcome back!\n");
        struct NewUserinfo user2;
            printf("What's your email? ");
            scanf("%s", e2);
            strcpy(user2.email, e2);
            printf("Your email is %s. What's your password? ", e2);
            scanf("%s", p2);
            strcpy(user2.pass, p2);
            printf("Your password is %s. ", p2);
            if(e1 == e2 && p1 == p2){
                printf("Welcome, %s.", name1);
            }
            else if(p1 != p2){
                printf("Wrong password.");
                goto alreadyUser;
            }
            else if(e1 != e2){
                printf("Invalid email.");
                goto alreadyUser;
            }
            else goto end;

    end:
        printf("Thank you for your time, %s. ", name1);
    getchar();
    return 0;
}

整个过程都很好,直到它到达登录部分,即使我输入相同的“电子邮件”和“通过”(在我的情况下,我只是在它们两个字面上都输入 gg)输出总是“错误”通行证”。如果我将 if 语句检查电子邮件放在 if 语句上方检查密码,它会立即更改为永久“无效电子邮件”,程序将不断要求我输入正确的“电子邮件”和“密码” .我在这里做错了什么吗?

【问题讨论】:

  • 旁注:永远不要不要以这种方式使用goto。您可以 [轻松] 用循环和 switch 语句替换所有实例。此外,避免函数范围 struct 声明。将你的移动到全局范围 [above main 定义]。要理解这一点,如果您必须拥有另一个使用 struct NewUserinfo 的函数,您会怎么做? (例如)如果您想将main 中的某些代码简化/拆分为单独的函数以提高模块化/清晰度,您将如何做到这一点?
  • 另外,将创建一个打印查询、读取 y/n、检查输入并返回结果的函数,这样会更简洁。

标签: c


【解决方案1】:

使用 strcmp。替换你所有的陈述:

if(e1 == e2 && p1 == p2){

通过

if((strcmp(e1, e2) == 0) && (strcmp(p1, p2) == 0)) {

您可能会找到有关 strcmp here 的有用参考。建议:避免使用命令 goto。更喜欢使用函数,它使您的代码更易于理解:)。

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2020-05-21
    • 1970-01-01
    • 2023-03-09
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多