Small Program written in C

Viewed 198

I'm new to programming in C and need help with my small assignment.

requirements of the C program:

  1. to ask a user to type his username and password on the command line input.
  2. program should demand at least two user attempts of inputting the passwords. your program should only terminate when the user has entered two identical passwords.
  3. program should store the username and password pair into a text file called “user.dat” in the current directory.

My Code seems to output to the file but i get this error in the command prompt window:

free(): double free detected in tcache2 Aborted

I also need to put a loop in the code somewhere when the passwords do not match to go back and prompt to re- enter, at the moment my program just
prints "Your passwords do not match please re-enter password i am not sure how to approach it.

Below is what i have written :

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

int main()
{
  char username [40];
  char pw [35];
  char confirmpw [35];

  FILE *fptr;

  fptr = fopen("user.dat", "w");

  // exiting program
  if (fptr == NULL) {
    printf("Error!");
    exit(1);
  }

  printf("Please Enter Your Username:\n");
  fgets(username, sizeof(username), stdin);
  fprintf(fptr, "%s", username);

  printf("please Enter Your Password:\n");
  fgets(pw, sizeof(pw), stdin);

  // Confirm password prompt
  printf("please Re-Enter to Confirm Your Password\n");
  fgets(confirmpw, sizeof(confirmpw), stdin);

  if(strcmp(pw, confirmpw) == 0){
    fprintf(fptr, "%s", pw);
    fclose(fptr);
  } else {
    printf("Your passwords did not match please re-enter your password \n");
    //need some type of loop ?
  }

  fclose(fptr);
  return 0;
}

3 Answers

Put a while loop instead of an if statement to request for new password until the confirm password is correct.

And notice you closed the file two times in your code, once in the if statement and second time at the end of main.

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

int main()
{
    char username [40];
    char pw [35];
    char confirmpw [35];


    FILE *fptr;


    fptr = fopen("user.dat", "w");

    // exiting program
    if (fptr == NULL)
    {
        printf("Error!");
        exit(1);
    }


    printf("Please Enter Your Username:\n");
    fgets(username, sizeof(username), stdin);
    fprintf(fptr, "%s", username);




    printf("please Enter Your Password:\n");
    fgets(pw, sizeof(pw), stdin);


    // Confirm password prompt

    printf("please Re-Enter to Confirm Your Password\n");
    fgets(confirmpw, sizeof(confirmpw), stdin);

    while(strcmp(pw, confirmpw) != 0)
    {
        printf("Your passwords did not match please re-enter your password \n");

        fgets(confirmpw, sizeof(confirmpw), stdin);
    }



    fprintf(fptr, "%s", pw);

    fclose(fptr);
    return 0;
}

let's start at the top. When I try to compile your program as written, I see that you're missing an #include:

t.c:40:6: warning: implicitly declaring library function 'strcmp' with type 'int (const char *, const char *)' [-Wimplicit-function-declaration]

To solve this I added

#include <string.h>

You should always expect your code to compile without any warnings. Don't suppress or ignore them, just fix them.

Now let's talk about your loop. My preferred way to loop until user input or other condition is met is with an infinite loop with a break inside a conditional. The infinite loop while(1) might seem a bit weird, but if you try to move the conditional test into the while() expression, you'll see why I say it's more convenient to do it this way.

my demo program of the loop:

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

int main()
{
  char* expected = "12345\n";
  char entered[25];
  while(1) {
    printf("please Enter '12345': ");
    fgets(entered, sizeof(entered), stdin);
    if(strcmp(expected, entered) == 0){
      break;
    } else {
      printf("Incorrect input.  Please try again.  \n");
    }
  }
  return 0;
}

here it is in action:

$ gcc -o t2 t.c && ./t2
please Enter '12345': 12
Incorrect input.  Please try again.
please Enter '12345': 45
Incorrect input.  Please try again.
please Enter '12345': 12345
$ 

A final note: my comparison string contains \n. That's because the newline from the "enter" key when entering text at the terminal gets passed into the program. That's not necessarily a problem for you because the newline is part of both strings to compare, but I did want to point out that the newline gets written into the destination file along with the rest of the password.

In case everything goes well, you are calling fclose() twice: the first one just after fprintf() and the second one just before return 0;. You should only call it once, but I guess it was a simple error and you already knew that. Internally fclose() may call free() which is why you are seeing that error.

As for your loop, I recommend using a do { ... } while ();. Inside the while condition you should put the call to strcmp().

Related