- Joined
- May 25, 2020
- Posts
- 1,560
- Solutions
- 1
- Reaction
- 4,132
- Points
- 899
C:
#include<stdio.h>
#include<conio.h>
#define ENTER 13
#define TAB 9
#define BCKSPC 8
#include<windows.h>
struct user{
char fullName[50];
char email[50];
char password[50];
char username[50];
char phone[50];
};
void takeinput(char ch[50]){
fgets(ch,50,stdin);
ch[strlen(ch) - 1] = 0;
}
char generateUsername(char email[50],char username[50]){
//abc123@gmail.com
for(int i=0;i<strlen(email);i++){
if(email[i] == '@') break;
else username[i] = email[i];
}
}
void takepassword(char pwd[50]){
int i;
char ch;
while(1){
ch = getch();
if(ch == ENTER || ch == TAB){
pwd[i] = '\0';
break;
}else if(ch == BCKSPC){
if(i>0){
i--;
printf("\b \b");
}
}else{
pwd[i++] = ch;
printf("* \b");
}
}
}
int main(){
system("color 0b");
FILE *fp;
int opt,usrFound = 0;
struct user user;
char password2[50];
printf("\n\t\t\t\t----------Welcome to authentication system----------");
printf("\nPlease choose your operation");
printf("\n1.Signup");
printf("\n2.Login");
printf("\n3.Exit");
printf("\n\nYour choice:\t");
scanf("%d",&opt);
fgetc(stdin);
switch(opt){
case 1:
system("cls");
printf("\nEnter your full name:\t");
takeinput(user.fullName);
printf("Enter your email:\t");
takeinput(user.email);
printf("Enter your contact no:\t");
takeinput(user.phone);
printf("Enter your password:\t");
takepassword(user.password);
printf("\nConfirm your password:\t");
takepassword(password2);
if(!strcmp(user.password,password2)){
generateUsername(user.email,user.username);
fp = fopen("Users.dat","a+");
fwrite(&user,sizeof(struct user),1,fp);
if(fwrite != 0) printf("\n\nUser resgistration success, Your username is %s",user.username);
else printf("\n\nSorry! Something went wrong :(");
fclose(fp);
}
else{
printf("\n\nPassword donot matched");
Beep(750,300);
}
break;
case 2:
char username[50],pword[50];
struct user usr;
printf("\nEnter your username:\t");
takeinput(username);
printf("Enter your password:\t");
takepassword(pword);
fp = fopen("Users.dat","r");
while(fread(&usr,sizeof(struct user),1,fp)){
if(!strcmp(usr.username,username)){
if(!strcmp(usr.password,pword)){
system("cls");
printf("\n\t\t\t\t\t\tWelcome %s",usr.fullName);
printf("\n\n|Full Name:\t%s",usr.fullName);
printf("\n|Email:\t\t%s",usr.email);
printf("\n|Username:\t%s",usr.username);
printf("\n|Contact no.:\t%s",usr.phone);
}
else {
printf("\n\nInvalid Password!");
Beep(800,300);
}
usrFound = 1;
}
}
if(!usrFound){
printf("\n\nUser is not registered!");
Beep(800,300);
}
fclose(fp);
break;
case 3:
printf("\t\t\tBye Bye :)");
return 0;
}
return 0;
}
Hindi ko alam lods saan yung error...