0

I am quite new to coding so please go easy on me

Here's my code

/* Open File*/
FILE *openFile(char filename[], char filetype[]);
/* Read Number Of Nurses */
int readNurses(void);
/* Title */
void title(void);
/* Login Screen*/
int loginScreen();
/* Checks if memory allocation was correct */
void mallocCheck(char **malloc1, char **malloc2);
/* Asks for login details */
int askLogin(void);
/* Retrieve Login Details */
void getDetails(char **properID, char **properPASS, int nurses);
/* Test Details Input To Details */
int testDetails(void);
/* Encryption Of Password */
void encryptPass(void);
/* Main Menu */
void mainMenu(void);
/* Encrypt Patient's Details */
void encrtpyPatient(void);
/* Enter Patients Details */
void enterPatients(void);
/* Create Patients File */
void createPatient(void);
/* Clear Screen Smooth */
void clearScreen(void); 

int main(void)
{
    int nurses;
    int correct;
    char enterID[ID_LENGTH];
    nurses = readNurses();
    correct = loginScreen(enterID, nurses);
    title();
    return 0;
}

void title(void)
{
    printf("\n\n\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =  Welcome to Action On Weight  =");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n                    *****************");
    printf("\n               ******               ******");
    printf("\n           ****                           ****");
    printf("\n        ****                                 ***");
    printf("\n      ***                                       ***");
    printf("\n     **           ***               ***           **");
    printf("\n   **           *******           *******           **");
    printf("\n   **           *******           *******           **");
    Sleep(SLEEP_TIME);
    printf("\n   **           *******           *******           **");
    printf("\n   **             ***               ***             **");
    printf("\n   **                                               **");
    printf("\n   **      *                                 *      **");
    printf("\n   **     **                                 **     **");
    printf("\n   **  ***                                     ***  **");
    printf("\n   **     *                                  *      **");
    printf("\n   **      ***                           ***        **");
    Sleep(SLEEP_TIME);
    printf("\n   ***      ****                       ****       ***");
    printf("\n     **         ******             ******         **");
    printf("\n      ***            ***************            ***");
    printf("\n        ****                                 ****");
    printf("\n           ****                           ****");
    Sleep(SLEEP_TIME); 
    printf("\n               ******               ******");
    printf("\n                    *****************");
    Sleep(SLEEP_TIME);
    Sleep(SLEEP_TIME);
}


int readNurses(void)
{
    FILE *fin;
    int nurses;
    fin = openFile("Nurses.txt", "r");
    while(!feof(fin))
    {
        fscanf(fin, "%*s");
        nurses++;
    }
    fclose(fin);
    printf("%d", nurses);
    return nurses;
}

FILE *openFile(char filename[], char filetype[])
{
    FILE *ptr = fopen(filename, filetype);
    if(!ptr)
    {
        printf("\n\n\n        Error 1. File couldn't be opened.");
              (" Please contact us at email@example.com");
              exit(1);
    }
    return ptr;
}

int loginScreen(char enterID[], int nurses)
{
    int correctDetails;
    int loop;
    char *properID;
    char *properPASS;
    properID = (char*) malloc(ID_LENGTH * nurses * 60);
    properPASS = (char*) malloc(PASS_LENGTH * nurses *60);
    correctDetails = 0;
    mallocCheck(&properID, &properPASS);
    getDetails(&properID, &properPASS, nurses);
    loop = nurses - 1;
    printf("das");
    while(nurses > 0)
    {
        printf("%s : %s", &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    /* do
    {
        printf("\n\n\n              ================================");
        printf("\n              =                              =");
        printf("\n              =         Login Screen         =");
        printf("\n              =                              =");
        printf("\n              ================================");
    }while(correctDetails = 0); */
    while(loop > 0)
    {
        free(properID - loop);
        free(properPASS - loop);        
        loop--;
    }
    return correctDetails;
}

void mallocCheck(char **malloc1, char **malloc2)
{
    if(malloc1 == NULL ||malloc2 == NULL)
    {
        printf("\n\n\n        Error 2. Assignment of ");
        printf(" memory wasn't succcesful.");
        printf(" Please contact us at email@example.com");
        Sleep(SLEEP_TIME);
        Sleep(SLEEP_TIME);
        exit(0);
    }
}

void getDetails(char **properID, char **properPASS, int nurses)
{
    FILE *ptr;
    ptr = openFile("Nurses.txt", "r");
    while(!feof(ptr))
    {
        fscanf(ptr, "%[^.]%*c%s\n", 
        &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    fclose(ptr);
}

The problem is in function loginScreen(), the function getDetails() get run, but then once it finishes 3221225477, can anyone point me out to why this is happening? It looks like I am allocating memory and everything properly. I have tried to find out why this is happening but I couldn't find anything.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Perhaps initialize nurses with 0. Also, what are you trying to count? – Vio Ariton Dec 16 '19 at 19:48
  • That's strange, because of the typo `while(correctDetails = 0)` in that function. – Weather Vane Dec 16 '19 at 19:48
  • Your pointer use is not correct in a few places. `getDetails(&properID, &properPASS, nurses);` You need to pass the pointers which contain the address of the allocated memory. Instead you are passing pointers to the pointers. That is, should be `getDetails(properID, properPASS, nurses);` – kaylum Dec 16 '19 at 19:48
  • 2
    Oh mybad: you commented that part out, but I also notice that `int loginScreen()` is one prototype that lacks a full declaration. Please post the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that shows the problem. This should also show the `#include` files you are using, and missing values such as `ID_LENGTH`. May I also suggest you take the [tour](https://stackoverflow.com/tour) and read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Weather Vane Dec 16 '19 at 19:50

2 Answers2

4

3221225477 is 0xC0000005 in hexadecimal, which is NTSTATUS code STATUS_ACCESS_VIOLATION. Your program has corrupted its working memory somehow. Compile your program on Linux instead; then you can run it under valgrind and be told exactly where the problem is.

(valgrind is such a valuable debugging tool that I recommend all C programmers keep a Linux installation around, even if they don't need it for anything else, just so they can use valgrind. It's never been ported to Windows, unfortunately for you. If you'd rather not spend several hours right now reconfiguring your computer for permanent dual-boot, you can get temporary access to Linux by booting from an Ubuntu liveCD.)

There's no reason you should have been expected to recognize 3221225477 as a Windows NT low-level error code. File a bug report on your IDE; it should be decoding these for you.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 2
    _"Compile your program on Linux instead..."_ That's a tall order! To install or live-boot a different operating system just for using a tool is quite an exaggeration to me. – machine_1 Dec 16 '19 at 20:13
  • @machine_1 valgrind is Just That Good. I'm not even joking. – zwol Dec 16 '19 at 21:44
0

If you have a look at the signature of fscanf() function, it says

The scanf() family of functions scans input according to a format as described below. This format may contain conversion specifiers; the results from such conversions, if any, are stored through the pointer arguments

and your getDetails() function expects three arguments; first tow are pointers to a character pointer, aka **char or *char[], and the last one is an int.

void getDetails(char **properID, char **properPASS, int nurses)

In your fscanf() call, you are passing the address of properID and properPASS arguments, even though these arguments are pointers themselves. If you update your fscanf() call as given below, your code should work fine.

fscanf(ptr, "%[^.]%*c%s\n", properID[nurses], properPASS[nurses]);
fnisi
  • 1,181
  • 1
  • 14
  • 24