1

I've made my code working ( closely to working ) but i've a problem. My registration work but the Login part works (reads) only for the First line... Let's say i have my file like this (each account for each line):

user1 pass1
user2 pass2
user3 pass3
user4 pass4

it will grant me a access only if i type the username as user1 and password as pass1, but not for user2 and pass2. how can i fix this ??? CODE:

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


void Login();
void Register();
char username[32];
char password[32];
char acc[32];
char pw[32];


int main()
{
    int logreg;
    printf("Press '1' For Login");
    printf("Press '2' For Register\n");
    logreg = getch();
    printf("\n------------------------------------------------\n");
tester:
    ;
    if (logreg == '1')
    {
        Login();
    }
    else if (logreg == '2')
    {
        Register();
    }
    else
    {
        printf("\nInvalid Input!!! Choose between '1' or '2' !!!\n");
        logreg = getch();
        goto tester;
    }
    return 0;
}

void Login()
{
start:
    ;
    char answer;

    // Vnesuvanje na username
    printf("\nEnter your Username: ");
    scanf("%s",username);

    // Vnesuvanje na Password
    printf("\nEnter your Password: ");
    scanf(" %s",password);




    FILE *fData;
    // Otvara file za citanje
    fData = fopen("database.txt", "rt");
    if (!fData)
    {
        printf("The file can not be opened\n\a\a");
    }

    int found=0;

        while(!feof(fData) && !found)
        {
            fscanf(fData, "%s\t%s", acc, pw);
            if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
            {
                setColor(LIGHTGREEN);
                printf("\nSuccessfuly logged it to our WebSite\n\n");
                setColor(GREY);
                found = 1;
                getch();
                break;
            }
            else if (!found)
            {
                setColor(LIGHTRED);
                printf("\nNo Access to our WebSite\n\n");
                printf("Invalid username or password!!!\n\n");
                setColor(GREY);
                printf("Would you like to try again?? [y/n] ");
                answer = getch();
                found = 0;
                printf("\n\n------------------------------------------------\n");
                break;
            }
        }

    fclose(fData);

tester2:
    ;
    //proverka za Povtorno pustanje na programata
    if (answer== 'y')
    {
        goto start;
    }
    else
    {
        if (answer!='n')
        {
            printf("Please choose between 'y' or 'n' !!!\n\n");
            answer = getch();
            goto tester2;
        }
        else
        {
            getch();
            return 0;
        }
    }

}

void Register()
{
    char acc[32];
    char pw[32];
    FILE *fData;
    fData = fopen("database.txt", "a");
    if (!fData)
    {
        printf("File could not be opened\n\a\a");
        getchar();
        return;
    }
    printf("Enter your desired Username: ");
    scanf("%s", acc);
    printf("Enter your desired Password: ");
    scanf("%s", pw);
    printf("\n");
    fprintf(fData, "%s\t%s\n", acc, pw);
    fclose(fData);
}

the problem should be somewhere here:

    FILE *fData;
    // Otvara file za citanje
    fData = fopen("database.txt", "rt");
    if (!fData)
    {
        printf("The file can not be opened\n\a\a");
    }

    int found=0;

        while(!feof(fData) && !found)
        {
            fscanf(fData, "%s\t%s", acc, pw);
            if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
            {
                setColor(LIGHTGREEN);
                printf("\nSuccessfuly logged it to our WebSite\n\n");
                setColor(GREY);
                found = 1;
                getch();
                break;
            }
            else if (!found)
            {
                setColor(LIGHTRED);
                printf("\nNo Access to our WebSite\n\n");
                printf("Invalid username or password!!!\n\n");
                setColor(GREY);
                printf("Would you like to try again?? [y/n] ");
                answer = getch();
                found = 0;
                printf("\n\n------------------------------------------------\n");
                break;
            }
        }

    fclose(fData);

but i dont know how do i fix the code, if some one who can fix it and paste it in a comment ( answer ) i would be so thankful. Anyway, thank you very much PS. Dont mind me, im not that good at programming but still willing to learn :)

Mitkashin
  • 93
  • 8

1 Answers1

0

Your logic is wrong.

If the username and the password do not match, then found is still 0 and then the statements in the else if clause will be executed.

    if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
    {
      printf("\nSuccessfuly logged it to our WebSite\n\n");
      found = 1;
      getch();
      break;
    }
    else if (!found)
    {
      printf("\nNo Access to our WebSite\n\n");
      printf("Invalid username or password!!!\n\n");
      printf("Would you like to try again?? [y/n] ");
      answer = getch();
      found = 0;
      printf("\n\n------------------------------------------------\n");
      break;
    }

You need to scan the whole file first and then only test if the user/password has been found.:

  while (!feof(fData) && !found)
  {
    fscanf(fData, "%s\t%s", acc, pw);
    if (strcmp(username, acc) == 0 && strcmp(password, pw) == 0)
    {
      printf("\nSuccessfuly logged it to our WebSite\n\n");
      found = 1;
      getch();
      break;
    }
  }

  if (!found)
  {
    printf("\nNo Access to our WebSite\n\n");
    printf("Invalid username or password!!!\n\n");
    printf("Would you like to try again?? [y/n] ");
    answer = getch();
    found = 0;
    printf("\n\n------------------------------------------------\n");
  }

BTW : Not directly related to your problem: Please read this SO article.

Community
  • 1
  • 1
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115