0

Basically, I am quite new to C Programming. I have created two Structs namely Item (for retrieving all my items from a text file with delimiters for display purposes)

struct Item
{
    char *itemcd;
    char *itemnm;
    char *desc;
    char *qqun;
};

And Item ID which retrieves the id from the same file but this one is for comparison purposes

struct ItemID
{
    char *itemcd;
};

This is my code for doing the comparison

    FILE *fp = NULL;
    int i = 0;
    struct Item var = { NULL, NULL, NULL, NULL };
    struct ItemID val = { NULL };
    char line[SIZE] = { 0 }, *ptr = NULL;
    char cd[10];

    /* 1. Open file for Reading */
    if (NULL == (fp = fopen("C:\\Users\\Arcraider\\Documents\\Visual Studio 2013\\Projects\\InventoryProject\\inventItems.txt", "r")))
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);
    }

    /* 2. Allocate Memory */
    var.itemcd = (char*)malloc(SIZE);
    var.itemnm = (char*)malloc(SIZE);
    var.desc = (char*)malloc(SIZE);
    var.qqun = (char*)malloc(SIZE);

    printf("=============================Purchase Items Available=======================================\n\n");
    /* 3. Read each line from the file */
    while (EOF != fscanf(fp, "%s", line))
    {
        /* 4. Tokenise the read line, using ":" delimiter*/
        ptr = strtok(line, ":");
        var.itemcd = ptr;

        while (NULL != (ptr = strtok(NULL, ":")))
        {
            i++;
            /* 5. Store the tokens as per structure members , where (i==0) is first member and so on.. */
            if (i == 1)
                var.itemnm = ptr;
            else if (i == 2)
                var.desc = ptr;
            else if (i == 3)
                var.qqun = ptr;
        }

        i = 0;        /* Reset value of i */
        printf("Item Code: [%s] Item Name: [%s] Description: [%s] Quantity: [%s]\n", var.itemcd, var.itemnm, var.desc, var.qqun);

    }
    fclose(fp);

    printf("\n\n============================================================================================\n\n");
    printf("Enter the item code which you would like:");
    fflush(stdin);
    scanf("%s", &cd);

    //validate data entry for id
    if (NULL == (fp = fopen("C:\\Users\\Arcraider\\Documents\\Visual Studio 2013\\Projects\\InventoryProject\\inventItems.txt", "r")))
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);
    }

    /* 2. Allocate Memory */
    val.itemcd = (char*)malloc(SIZE);


    /* 3. Read each line from the file */
    while (EOF != fscanf(fp, "%s", line))
    {
        /* 4. Tokenise the read line, using ":" delimiter*/
        ptr = strtok(line, ":");
        val.itemcd = ptr;

        while (NULL != (ptr = strtok(NULL, ":")))
        {
            i++;
            /* 5. Store the tokens as per structure members , where (i==0) is first member and so on.. */
        }
        i = 0;        /* Reset value of i */
        printf("\nItem Code: %s\n", val.itemcd);
        if (cd == val.itemcd){
            printf("Code Match Successful!!!");
            break;
            _getch();
        }
        else if (cd != val.itemcd)
        {
            printf("Matching......Code not present. Failed\n");
        }
    }
    _getch();

When run the code i keep getting a result that "Matching......Code not present. Failed" even though i enter the correct value "K1234" for testing. Please help me do the comparison part.

The image of the error is:

enter image description here

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Don
  • 25
  • 7
  • 5
    you cannot compare strings like that. You need `strcmp`. Who upvotes such sunday morning questions BTW? – Jean-François Fabre Oct 02 '16 at 08:20
  • thanks so much for the feedback Jean-François Fabre cn u show me an example of how i can implement the comparison in C since i am still relatively new to it – Don Oct 02 '16 at 08:39
  • 1
    http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c#8004250 – matt Oct 02 '16 at 09:05
  • Thnks so much bro. i shall utilize this..you are a real life saver – Don Oct 02 '16 at 09:17

0 Answers0