vendredi 22 janvier 2021

My code terminates after the if statement

My code ignores my the else statement of the if and else and just terminates after it doesn't fulfill the if its supposed to move to the else but it just terminates. even the while statement I put there is being ignored I'm still just learning c code so any type of help will be appreciated. and its kind of urgent

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

int ReadNextInt(FILE *f)
{
    int n;
    fscanf(f,"%d",&n);

    char ch;
    fscanf(f,"%c",&ch);
    return n;
}

float ReadNextFloat(FILE *f)
{
    float n;
    fscanf(f,"%f",&n);

    char ch;
    fscanf(f,"\n",&ch);

    return n;
}

void ReadNextString(FILE *f, char temp[])
{
    char ch;
    int i = 0;
    fscanf(f, "%c", &ch);

    while(ch == ' ')
        fscanf(f, "%c", &ch);

    while (ch != ',' && ch != '\n')
    {
        temp[i] = ch;
        i++;
        fscanf(f, "%c", &ch);
    }
    temp[i] = '\0';
    return;
}


int AuthValidation(char fullName[], char un[], char pw[])
{
    FILE *f = fopen("D:\\teller_File.txt","r");

    char tFullName[30];
    char tUn[30];
    char tPw[30];

    while (!feof(f))
    {
        ReadNextString(f, tFullName);
        ReadNextString(f, tUn);
        ReadNextString(f, tPw);


        if (strcmp(tUn, un) == 0  && strcmp(tPw, pw) == 0)
        {
            strcpy(fullName, tFullName);
            fclose(f);
            return 1;
        }
    }

    fclose(f);
    return 0;
}

int main()
{
    char full[30] = "";
    char userInput1[30] = "";
    char userInput2[30] = "";

    printf("Enter username: ");
    scanf("%s", userInput1);

    printf("Enter password: ");
    scanf("%s", userInput2);

    if (AuthValidation(full, userInput1, userInput2))
    {
        printf("Welcome %s!\n", full);
    }
    else
    {
        printf("Authentication failed!\n");
    }


    //FILE *f = fopen("C:\\Users\\Public\\info.txt","r");

    /*
    char custNumber[100];
    char custName[100];

    ReadNextString(f, custNumber);
    printf("%s\n", custNumber);

    ReadNextString(f, custName);
    printf("%s\n", custName);

    int num;
    num = ReadNextInt(f);
    printf("%d\n", num);

    float fnum;
    fnum = ReadNextFloat(f);
    printf("%.2f\n", fnum);

    ReadNextString(f, custNumber);
    printf("%s\n", custNumber);
    */

    //fclose(f);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire