1

I'm trying to make a simple program which asks for the user to input a name, email and password as a part of signing up, then make him enter the email and password again to log in.

#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;
}

The whole thing works just fine until it reaches the login part, where even when I enter the same "email" and "pass" (in my case I just type gg in literally both of them) the output is always "Wrong passord". And if I put the if statement to check the emails above the if statement to check the passwords, it immediately changes to permanent "Invalid Email" where the program will continuously ask me to input what should be the correct "email" and "password". Is there anything that I'm doing wrong here?

3
  • Use strcmp: stackoverflow.com/questions/19479232/… Commented Sep 17, 2021 at 18:41
  • Side note: Do not ever use goto in this manner. You can [easily] replace all instances with a loop and switch statements. Also, avoid function scoped struct declarations. Move yours to global scope [above the main definition]. To understand this, what would you do if you had to have another function that used struct NewUserinfo? (e.g.) If you wanted to simplify/split off some code in main into separate functions to improve modularity/clarity, how would you do that? Commented Sep 17, 2021 at 19:06
  • also, would create a function that print inquery, read y/n, checks input and returns the result, would be a lot cleaner. Commented Sep 17, 2021 at 20:26

1 Answer 1

3

Use strcmp. Substitute all your statements:

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

by

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

You may find a usefull reference for strcmp here. Suggestion: avoid using the comand goto. Prefer using functions, it make your code simpler to understand :).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.