I'm new to programming in C and need help with my small assignment.
requirements of the C program:
- to ask a user to type his username and password on the command line input.
- 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.
- 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;
}