0

I need to make a journal/planner where I can have a username and password login and set up a login and account if you already don't have one. I need to then later be able to login using that and access my journal. In there I can put in entry's and save it. So that when I quit the program and start the program again, once I login, I can access the planner again.

So my problem is that for setting up my account, I need to allow the user to enter a username and password, but when I start the program, it skips the part where I can type in my username and goes straight to the password. The code:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000

void signUp (char username [SIZE], char password [SIZE], char name [SIZE], int age) {
    printf("Sign Up Procedure: \n");
    printf("\nPlease enter your username: ");
    gets(username);
    printf("\nPlease enter a password: ");
    gets(password);
    printf("\nEnter your full name: ");
    gets(name);
    printf("\nEnter your age: ");
    scanf("%d", &age);

    printf("Username: %s, Password: %s, Full Name: %s, Age: %d", username, password, name, age);
}

void logIn(char username [SIZE], char password [SIZE]) {

}

int main() {
    printf("1. Log In\n");  // Enter 1 for logging in
    printf("2. Sign Up\n"); // Enter 2 for signing up
    int choice;
    scanf("%d", &choice);

    char username [SIZE];
    char password [SIZE];
    char name [SIZE];
    int age;

    int keepGoing = 0;

    while (keepGoing == 0){
        switch (choice) {
        case (1):
            logIn (username, password);
            keepGoing = 1;
            break;
        case (2):
            signUp (username, password, name, age);
            keepGoing = 1;
            break;
        default:
            printf("Please enter a choice from the given options");
            keepGoing = 0;
            break;
        }
    }

    FILE * pfile;

    pfile = fopen("Planner.txt", "w");

    fputs("Planner", pfile);

    fclose(pfile);

    return 0;
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31

1 Answers1

1

Your first scanf()

scanf("%d", &choice);

is reading an integer, so it leaves the newline character in the input buffer.

Then when you try to get the username:

gets(username);

It picks up that newline character and assigns it to username. I would suggest reading up on C input and why scanf() can be dangerous and tricky to use, especially when mixing integer and string input and when using it with other C input functions.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31