0

After making the said changes, a number of other issues have occurred. One is that the only user that can log in is the first one in the text file. I apologize if the solution is simple, I am new to coding. the text file of users contains a list of usernames and passwords formatted as

USERNAME:PASSWORD \n USERNAME2:PASSWORD2

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

#define NAMELENGTHS 20
#define TEXTLENGTHS 200
#define PASSWORDS 8

struct patients
{
char username[NAMELENGTHS];
char password[PASSWORDS];    
};

void emptyBuffer(void);
void addDetails(struct patients list[], int i);


int main(void)
{
FILE *fin;
struct patients list[20];
char usernameIN[NAMELENGTHS];
char userInput[PASSWORDS];
int noPatients = 0, i = 0;
if(!(fin = fopen("passwords.txt", "r")))
{
    printf("File not detected\n");
    return 1;
}
while(fscanf(fin,"%19[^:]:%7[^\n]", list[noPatients].username,
      list[noPatients].password)
     == 2)
{
    noPatients++;
}
fclose(fin);
printf("please enter a username: ");
scanf("%s", usernameIN);
emptyBuffer();
printf("Please enter a password: ");
scanf("%s", userInput);
emptyBuffer();
while(i < noPatients)
{
    if(strcmp(usernameIN, list[i].username) == 0 &&
       strcmp(userInput, list[i].password) == 0)
       {
           printf("Log in successful.\n");
           int menuOption;
           do
           {
               printf("\n--------------------------------\n");
               printf("Main Menu");
               printf("\n--------------------------------\n");
               printf("1. Add Details\n");
               printf("2. Read Details\n");
               printf("3. Exit program\n");
               scanf("%d", &menuOption);
               emptyBuffer();
               switch(menuOption)
               {
                   case 1:
                        addDetails(list, noPatients);
                        break;
                   case 2:
                        printf("option 2");
                        break;
                   case 3:
                        return 0;
                   default:
                        printf("\nInvalid Input");
                        break;
                }
           }while(menuOption != 3);
       }
    i++;
}
printf("log in failed");
return 0;
}
  • 1
    Can you create a [mre]? – Sourav Ghosh Dec 21 '20 at 14:10
  • thanks didn't notice that, the issue I'm having is with the add detail function but that still helped! – MayhemDaes Dec 21 '20 at 14:18
  • 3
    Don't put `\n` in `scanf` format strings. – dbush Dec 21 '20 at 14:18
  • that's worked, thank guys! sorry if it seemed stupid, I'm new to programming... – MayhemDaes Dec 21 '20 at 14:20
  • Line 46: `list[noPatients++];` What are you trying to do there - its not assigning anything.. Should just be an increment: `noPatients++`. – costaparas Dec 21 '20 at 14:21
  • Also, line 75 should be `addDetails(list, i);` You are passing `noPatients` which is a bug. – costaparas Dec 21 '20 at 14:22
  • 1
    Do try and be a little bit more generous with your buffers. 6 characters for a password is really tight. – tadman Dec 21 '20 at 14:23
  • Where is `noPatients` declared in main()? – Krishna Kanth Yenumula Dec 21 '20 at 14:23
  • Also, your comparison on line 57-58 should be `&&` -- in order to confirm the username AND password are both correct. Otherwise, you can give a valid username and login with any password... (or vice versa - but the former is more concerning) – costaparas Dec 21 '20 at 14:24
  • when I add "&&" it doesn't allow anyone but the 1st user in the text file to log in, so I tried using !=, but it still lets anyone login in with a correct username, or password, so I left it as is hoping to find a work around later – MayhemDaes Dec 21 '20 at 14:33
  • 1
    The trailing white space in a `scanf()` format string (blank _and_ newline in this example; one is sufficient!) is a UI disaster — as discussed in the duplicate. The input hangs waiting for you to type something that isn't white space. – Jonathan Leffler Dec 21 '20 at 14:38
  • `noPatients` is not defined and is not initialized. You are copying it to `i` in the function. So, value of `i` is unknown. And you are trying to store value at `list[i].forename`. This causes crash. – Krishna Kanth Yenumula Dec 21 '20 at 15:05
  • i fixed the crashing issue, the only remaining issue is the fact that I can only log in at the first user in the text file – MayhemDaes Dec 21 '20 at 15:11
  • The code here is not **minimal** - please reduce it to the _shortest_ code that demonstrates the problem. – Toby Speight Dec 24 '20 at 09:29

0 Answers0