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?

Chroma
  • 23
  • 3
  • Use `strcmp`: https://stackoverflow.com/questions/19479232/comparing-two-strings-problems-with-strcmp – Dai Sep 17 '21 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? – Craig Estey Sep 17 '21 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. – Tomer W Sep 17 '21 at 20:26

1 Answers1

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 :).

cj-2307
  • 259
  • 3
  • 14