Thread: Trying to pass back up the Quantity of items sold.

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    3

    Trying to pass back up the Quantity of items sold.

    I honestly have no idea where I'm going wrong. I'm trying to retrieve an integer for items sold by comparing strings of text in the quantitySold function. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    
    FILE *csis;
    
    
    typedef struct{
        char itNo[MAX], itDes[MAX], itSal[MAX];
        float itAmo;
        int itQua;
    } database;
    
    
    int     main(void);
    int     inputItemData(database ItemInfo[]);
    int     inputItemData1(database ItemInfo[]);
    void    outputData(database ItemInfo[], int num);
    int     quantitySold(database ItemInfo[], int num1, int num, int sum[MAX]);
    void    strsub(char *buf, char *s, int start, int end);
    
    
    int main(void) {
        database ItemInfo[MAX];
        int num, num1, num2, sum[MAX];
        float x;
    
    
        csis = fopen("SalesSummary.txt", "w");
        if (csis == NULL){
            puts("File SalesSummary.txt could not be opened for output.");
            exit(1);
        }
    
    
        num = inputItemData(ItemInfo) - 1;
        num1 = inputItemData1(ItemInfo) - 1;
        outputData(ItemInfo, num);
        printf("%d", ItemInfo[2].itQua);
        fclose(csis);
        return 0;
    }
    
    
    int inputItemData(database ItemInfo[]) {
        int i=0;
        char buf[MAX];
        char temp[MAX];
        FILE *fp;
    
    
        fp = fopen("ItemList.txt", "r");
        if (fp == NULL) {
            puts("File ItemList.txt could not be opened for input.");
            exit(1);
        }
        while (!feof(fp)){
            fgets(buf, MAX, fp);
            strsub(buf, ItemInfo[i].itNo, 0, 6);
            strsub(buf, ItemInfo[i].itDes, 8, 20);
            strsub(buf, temp, 22, 26);
            ItemInfo[i].itAmo=atof(temp);
            i++;
        }
        fclose(fp);
    
    
        return i;
    }
    
    
    int inputItemData1(database ItemInfo[]) {
        int i=0;
        char buf[MAX];
        char temp[MAX];
        FILE *fp;
    
    
        fp = fopen("ItemSales.txt", "r");
        if (fp == NULL) {
            puts("File ItemSales.txt could not be opened for input.");
            exit(1);
        }
        while (!feof(fp)){
            fgets(buf, MAX, fp);
            strsub(buf, ItemInfo[i].itSal, 0, 6);
            i++;
        }
        fclose(fp);
    
    
        return i;
    }
    
    
    void outputData(database ItemInfo[], int num) {
        int i;
    
    
        printf("Item Number\tItem Description\tQuanity Sold\n");
        fprintf(csis, "Item Number\tItem Description\tQuanity Sold\n");
        for(i=0; i<num; i++) {
            printf("%s\t", ItemInfo[i].itNo);
            printf("\t%s\t", ItemInfo[i].itDes);
            printf("\t%.2f \n", ItemInfo[i].itAmo);
            fprintf(csis, "%s\t", ItemInfo[i].itNo);
            fprintf(csis, "\t%s\t", ItemInfo[i].itDes);
            fprintf(csis, "\t%.2f \n", ItemInfo[i].itAmo);
        }
    }
    
    
    int quantitySold(database ItemInfo[], int num1, int num, int sum[MAX]){
    
    
        int i, x;
        for(x=0; x<num1; x++){
                for(i=0; i<num; i++){
        if(strncmp(ItemInfo[x].itSal, ItemInfo[i].itNo) == NULL){
            ItemInfo[i].itQua = ItemInfo[i].itQua + 1;
        }
        }
    } return ItemInfo[i].itQua;
    }
    
    
    
    void strsub(char buf[], char sub[], int start, int end) {
        int i, j;
    
    
        for (j=0, i=start; i<=end; i++, j++)
            sub[j] = buf[i];
        sub[j] = '\0';
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,543
    Did you try reading some documentation for the strncmp() function?

    What does this function return?

    Does it ever return NULL?

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..

    I have looked twice now and yet to find a call to that function....where did I miss it ?

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Did you try reading some documentation for the strncmp() function?

    What does this function return?

    Does it ever return NULL?

    Jim
    That's what I'm unsure of. I'm trying to compare strings and if they match I want them to add a 1. I've accomplished quite a bit, but that's the only thing I can't overcome. All I want is a function to compare strings and add up in an array, having different numerical values. I'm not quite sure how to explain it. I've been working on this the past 12 hours. I can't necessarily say I'm thinking straight.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    Quote Originally Posted by JohnGM View Post
    Hi ..

    I have looked twice now and yet to find a call to that function....where did I miss it ?
    I wanted to store the values in an array. I didn't necessarily call the function in the main, but used it to store numerical values.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,543
    Did you read the link I posted?

    And from that link:
    Return Value
    Returns an integral value indicating the relationship between the strings:
    Code:
    return value	         indicates
       <0                the first character that does not match has a lower value in str1 than in str2
        0                the contents of both strings are equal
       >0                the first character that does not match has a greater value in str1 than in str2
    Please note that NULL is not one of the items that is returned by this function. So the following snippet is incorrect:
    Code:
        if(strncmp(ItemInfo[x].itSal, ItemInfo[i].itNo) == NULL){
    As you should see from the documentation this function returns zero if the strings are equal, non-zero if the strings are not equal.

    Jim
    Last edited by jimblumberg; 12-14-2015 at 11:15 PM.

  7. #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't know how to sold this exersise
    By dontoo in forum C Programming
    Replies: 2
    Last Post: 03-29-2010, 05:10 AM
  2. Confused as to how to pass back to main
    By J-Camz in forum C Programming
    Replies: 6
    Last Post: 11-28-2008, 07:21 AM
  3. How to pass locations of bit-field items?
    By dan56965 in forum C Programming
    Replies: 1
    Last Post: 08-14-2008, 01:55 PM
  4. Shifting back <vector> items...
    By aker_y3k in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2003, 10:30 AM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM