๐Ÿ”’ Closed C Programming pa help po nito

Status
Not open for further replies.

XsanX

๐‘ฉ๐‘ถ๐‘น๐‘ต ๐‘ป๐‘ถ ๐‘ฉ๐‘ฌ ๐‘ต๐‘ถ๐‘ป๐‘ฏ๐‘ฐ๐‘ต๐‘ฎ
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...
 
The error in this code is in the generateUsername function. The function is missing a return statement. Because of this, when the function is called, it doesn't return a value. This causes the fwrite statement that comes after the function call to write uninitialized data. This will cause your program to crash or produce unexpected results.

The correct version of the function should return the generated username:
char* generateUsername(char email[50],char username[50]){
//abc123@gmail.com
for(int i=0;i<strlen(email);i++){
if(email == '@') break;
else username = email;
}
return username;
}

Also it is better to use strcpy instead of memset.
strcpy(username,email);
username[strcspn(username,"@")] = '\0';


this will make sure that the username only contains the string before the @ symbol.

And Also it is better to use strcmp instead of !strcmp, because it increase readability.




Soul Calibre | Guardian Angel
"Life is TOUGH but I don't give a shit because I know I am AWESOME"

C:
#include<stdio.h>
#include<string.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];
    }
    return username;
}

void takepassword(char pwd[50]){
    int i = 0;
    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","ab");
                if(fwrite(&user,sizeof(struct user),1,fp) == 1)
                    printf("\n\nUser registration 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;
}
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 1
    Replies
  • 552
    Views
  • 2
    Participants
Last reply from:
Soul Calibre

Online now

Members online
578
Guests online
719
Total visitors
1,297

Forum statistics

Threads
2,276,932
Posts
28,973,130
Members
1,230,623
Latest member
norbertandyoda
Back
Top