-2

So this is my code for an assignment for school, and right now my problem is in my inputID function. Where the comment says "If the same!!!!!!!!!!!!!!!!!!!!!!!!!!!", I try to compare a string given by the user and a string stored in my array of strings "IDArray". I try using the strcmp function however I keep getting an error. Any help with this would be much appreciated. The contents of the text file it reads from is shown below the code. Thank You

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000
#define MAX_INPUT 40

void writeFile();
void inputPass();
void inputID();
void userInput();
void printDB();
void readFile();
void inputInit();
void DBInit();
void init();

FILE *fp;

char **IDArray;
char **passwordArray;

char *IDInput;
char *passInput;

int main()
{
    init();
    readFile();

    printf("\n\n\tWelcome to CPS_633, Lab 1\t\n\n");

    userInput();
    writeFile();
    printDB();

    return 0;
}

void writeFile()
{
    fp = fopen("Database_Table.txt", "w");
    int i;
    for (i = 0; i < MAX_RECORDS; i++)
    {
        if (IDArray[i][0] != '\0')
        {
            fprintf(fp, "%s\t%s\n", IDArray[i], passwordArray[i]);
        }
        else
        {
            break;
        }
    }
    fclose(fp);
}

void printDB()
{
    printf("\nUsername\tPassword\n");
    int i;
    int databaseLength = 0;

    for (i = 0; i <= MAX_RECORDS; i++)
    {
        if (IDArray[i][0] != '\0')
        {
            printf("%s\t\t%s\n", IDArray[i], passwordArray[i]);
        }
        else
        {
            break;
        }
    }
}

void inputPass()
{
    int correct = 1, strLength;

    printf("Please Enter Your Password (no special characters): ");
    fgets(passInput, MAX_INPUT, stdin);
    strLength = strlen(passInput) - 1;

    if (strLength > 0 && passInput[strLength] == '\n')
    {
        passInput[strLength] = '\0';
    }

    while (correct)
    {
        int k;
        int specialCase = 1;
        for (k = 0; k <= strLength; k++) //Searches for special characters
        {
            switch (passInput[k])
            {
            case '~':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '!':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '`':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '@':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '#':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '$':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '%':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '^':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '&':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '*':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '(':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case ')':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '-':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '_':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '=':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '+':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '[':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case ']':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '{':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '}':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '|':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '\\':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case ':':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case ';':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;

            case '"':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case ',':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '<':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '.':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '>':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '?':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            case '/':
                printf("Special Character(s) Found\n");
                specialCase = 0;
                break;
            }
            if (specialCase == 0)
                break;
        }
        if (specialCase != 0)
        {
            if (strLength > 12) //Password longer than 12 characters
            {
                printf("This password is too long, the characters after the 12th has been cut off\n");
                int i;
                for (i = 0; i < MAX_RECORDS; i++)
                {
                    if (passwordArray[i][0] == '\0')
                    {
                        strncpy(passwordArray[i], passInput, 12);
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                correct = 0;
            }
            else //Password shorter than 12 characters
            {
                int i, j;
                for (j = strLength; j <= 12; j++) //Pads password with "-"
                {
                    passInput[j] = '-';
                }

                for (i = 0; i < MAX_RECORDS; i++) // Traverses array for first empty slot
                {
                    if (passwordArray[i][0] == '\0') //If empty insert
                    {
                        strncpy(passwordArray[i], passInput, 12);
                        break;
                    }
                    else // If not empty continue
                    {
                        continue;
                    }
                }
                correct = 0;
            }
        }
        else
        {
            printf("Please Enter Your Password (no special characters): ");
            fgets(passInput, MAX_INPUT, stdin);
            strLength = strlen(passInput) - 1;

            if (strLength > 0 && passInput[strLength] == '\n')
            {
                passInput[strLength] = '\0';
            }
        }
    }
}

void inputID()
{
    int correct = 1, strLength;

    printf("Please Enter Your Username ID: ");
    fgets(IDInput, MAX_INPUT, stdin);
    strLength = strlen(IDInput) - 1;

    if (strLength > 0 && IDInput[strLength] == '\n')
        IDInput[strLength] = '\0';

    while (correct)
    {
        if (strLength > 32) //If longer than 32 characters
        {
            printf("This Username ID is longer than 32 characters\n");
            printf("Please Enter Your Username ID: ");
            fgets(IDInput, MAX_INPUT, stdin);

            strLength = strlen(IDInput) - 1;

            if (strLength > 0 && IDInput[strLength] == '\n')
                IDInput[strLength] = '\0';
        }
        else if (strLength < 4) //If shorter than 4 characters
        {
            printf("This Username ID is shorter than 4 characters\n");
            printf("Please Enter Your Username ID: ");
            fgets(IDInput, MAX_INPUT, stdin);

            strLength = strlen(IDInput) - 1;

            if (strLength > 0 && IDInput[strLength] == '\n')
                IDInput[strLength] = '\0';
        }
        else //If acceptable length
        {
            int i;
            for (i = 0; i < MAX_RECORDS; i++) 
            {
                if (IDArray[i][0] != '\0') //If element occupied, compare
                {
                    if (strcmp(IDArray[i], inputID) == 0) // If the same!!!!!!!!!!!!!!!!!!
                    {
                        printf("Found Match");
                        correct = 0;
                        break;
                    }
                    else //If not the same
                    {
                        continue;
                    }
                }
                else //If element empty, insert
                {
                    strcpy(IDArray[i], IDInput);
                    break;
                }
            }
            correct = 0;
        }
    }
}

void userInput()
{
    inputID();
    inputPass();
}

void readFile()
{
    fp = fopen("Database_Table.txt", "r");

    char line[MAX_INPUT];
    if (fp == NULL)
    {
        perror("Error in opening file");
    }
    else
    {
        int i = 0;
        while (!feof(fp))
        {
            if (fgets(line, sizeof(line), fp) == NULL)
            {
                break;
            }
            else
            {
                sscanf(line, "%s\t%s", IDInput, passInput);
                strcpy(IDArray[i], IDInput);
                strcpy(passwordArray[i], passInput);
                i++;
            }
        }
    }
    fclose(fp);
}

void inputInit()
{
    IDInput = (char *)malloc(sizeof(char) * MAX_INPUT);
    passInput = (char *)malloc(sizeof(char) * MAX_INPUT);
}

void DBInit()
{
    IDArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);
    passwordArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);

    int i, j;
    for (i = 0; i < MAX_RECORDS; i++)
    {
        IDArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
        passwordArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
        for (j = 0; j < MAX_INPUT; j++)
        {
            IDArray[i][j] = '\0';
            passwordArray[i][j] = '\0';
        }
    }
}

void init()
{
    DBInit();
    inputInit();
}

ID11 PASSWORD1

ID22 PASSWORD2

ID33 PASSWORD3

ID44 PASSWORD4

ID55 PASSWORD5

ID55 PASSWORD5

  • Also please be gentle with how messy my code is :} – Andy Steven Llactahuamani Oct 02 '16 at 23:47
  • 1
    Nuke and rebuild. NEVER NEVER store passwords in plaintext for verification. It's an assignment you say. Complain the assignment teaches really bad habits. – Joshua Oct 02 '16 at 23:49
  • No, I understand that it's bad habit to do that. I haven't applied the hashing function yet. This is for a security class loll – Andy Steven Llactahuamani Oct 02 '16 at 23:50
  • The best I can recommend is apply printf debugging. – Joshua Oct 02 '16 at 23:55
  • 1
    Please try and create an MVCE (http://stackoverflow.com/help/mcve). Also, whenever you want to know why something is causing an error, you need to give the full error message... – Ben Wainwright Oct 02 '16 at 23:57
  • Doesn't the 30 repeats of almost the same code in `inputPass()` make you cry? It does me. What about control characters (tabs, etc)? Have you come across `` and functions/macros like `ispunct()`? It's doubly bad when you force us to look at 120 lines of code when half a dozen would do the job — that's 20 times as much code as necessary to read and ignore (hoping that the problem isn't an inconsistency in that block of code!). Even if you listed 30 cases and 3 lines of code after them, it would be 33 lines vs 120). Please read about how to create an MCVE ([MCVE]), as already requested. – Jonathan Leffler Oct 03 '16 at 01:34
  • 1
    Also, the actual problem, correctly diagnosed by [deamentiaemundi](http://stackoverflow.com/users/4241278/deamentiaemundi), is that you're comparing a function pointer with a string. The compiler should be shrieking at you 'incompatible types'. If it is, don't forget to mention it; that's your problem. At this stage in your career as a C programmer, the compiler doesn't make mistakes and if it gives you a warning, it needs to be fixed. It will be a while before you can afford to ignore warnings — I seldom ignore any, but I've only been coding C for 30 years so there's lots left to learn. – Jonathan Leffler Oct 03 '16 at 01:37
  • 1
    Oh, and if the compiler isn't shrieking at you about your mistake, you either need to turn on more warnings or you need to get a better compiler. The code should not be compiling without at least warnings. Were I to try compiling it under my default compilation flags, it would fail on that (and a number of other issues of greater or lesser significance). – Jonathan Leffler Oct 03 '16 at 01:40

1 Answers1

0

You have a typo in line 325 (the line with that comment): it's IDInput you want to compare with, not inputID which is the name of the function it's in.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20