Reading and Displaying Data Files, Correct results, but code is unfeasible

129 views Asked by At

I have a program in class that reads a data file for a store and then prints them out on a prompt.

1 Suits    300 100  92        
1 Coats    200  60  65
1 Shirts  1000  12  13
2 Dresses  400  60  65
2 Coats    185 184 200
2 Shoes    600  40  30
3 Jeans    200  40  35  
3 Shoes    200  30  34
4 Jeans    300  42  43

The numbers being department, item name, quantity, cost to buy, and cost to sale.

#include <stdio.h>
#include <stdlib.h>
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif

int main()
{
    FILE *in;
    char item[8];
    int department, quantity, prev = 1,k=0;
    float cost, market, cost2, mark2, total, totcost, totmark = 0, lowest;


    if((in = fopen("blinn.dat", "r")) == NULL)
    {
        printf ("Can't open file blinn.dat\n");
        system("pause");
        exit(1);
    }
    else
    {
        printf ("\n\n\t\t\t\t  Blinn Apparel Store");
        printf ("\n\n\n\t\t        Unit Cost                 Extended");
        printf ("\n             Quantity     Cost  Market         Cost      Market   Lower Cost\n");
        printf ("\nMens Dept");
        int m=0;
        while(fscanf(in, "%d %s %d %f %f", &department, item, &quantity, &cost, &market) != EOF)
        {
            if(department != prev)
            {
                lowest = min(totcost, totmark);
                printf ("\n  Total\t\t\t\t            $%8.2f  $%8.2f  $%8.2f", totcost, totmark, lowest);
                totcost = 0;
                totmark = 0;
                prev = department;
                total += lowest;
                switch (m)
                {
                    case 0:
                        printf("\nLadies Dept");
                        break;
                    case 1:
                        printf("\nGirls Dept");
                        break;
                    case 2:
                        printf("\nBoys Dept");
                        break;
                }
                m++;
            }

            if (department == 1)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d   %8.2f   %4.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 2)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d   %8.2f%8.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 3)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d    %8.2f  %5.2f       %8.2f   %8.2f   ",
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 4)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d    %8.2f  %5.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
                lowest = min(totcost, totmark);
                printf ("\nTotal\t\t\t\t\t    $%8.2f  $%8.2f  $%8.2f\n", totcost, totmark, lowest);
                total += lowest;
            }
        }
    }
    printf("Inventory at lower cost\t\t\t\t\t         $%8.2f\n", total);
    system ("pause");
}a

Disregard the nonuniform spacing for each line; I was troubleshooting a large number I was inexplicably getting. I have the code done all nice and dandy and it all checks out, but my professor has given me half credit because of the if statements, saying if there were a large number of departments my code wouldn't be feasible. He said I could replace it with one statement and that threw me off because I've tried working it into the switch statement, but that obviously doesn't work because some of the clothing items don't appear.

I just can't seem to change things without the math going crazy. My first thought was to make a function for the multiple additions and multiplications to totmark, totcost, cost2, and mark2, but anytime I disrupt it, everything falls apart and I can't seem to put it back together.

This should be an easy fix, but I appreciate any help.

3

There are 3 answers

3
Saurabh Meshram On BEST ANSWER

I think they expect you to make use of function.
You could also use Structures.

/* Structure Definition */
typedef struct goods                                                            
{                                                                               
    int dept;                                                                   
    char item[64];                                                             
    int quantity;                                                               
    int sell_price;                                                             
    int cost_price;                                                             
} goods_t; 

and create an array of structures (in main()), like this

/* Array of Structure */
#define NO_ENTRIES 10   /* No of entries in your file */
goods_t arr[NO_ENTRIES];
for(i = 0; i< TOTAL_DEPT; i++)
{
    fscanf(fp, "%d %s %d %d %d", &arr[i].dept, arr[i].item, &arr[i].quantity, &arr[i].sell_price, &arr[i].cost_price);
    cal(arr[i].dept, arr[i].item, arr[i].quantity, arr[i].sell_price ,arr[i].cost_price);
}

Also you could write a generic function to calculate:

void calculate (int dept, char *item, int quantity, int sp, int cp)
{                                                                               
    int total_cost = 0, total_market = 0;
    total_cost += quantity * cp;
    total_sell += quantity * sp;
    ...
    printf("%s\t%d\t%d\t%d\n", item, quantity, sp, cp);                         
    printf("Total : %d\t %d\n", total_cost, total_sell);
    ...
    return;
}

Side Note:
You could use enum

enum type{                                                                      
    mens = 0,                                                                   
    ladies,                                                                     
    boys,                                                                       
    girls,                                                                      
}; 

and add the following to function calculate

if(dept == mens)                                                            
    printf("Mens Dept");                                                    
else if(dept == ladies)                                                     
    printf("Ladies Dept");                                                  
else if(dept == boys)                                                       
    printf("Boys Dept");                                                    
else                                                                        
    printf("Girls Dept"); 
0
user3629249 On
the following compiles with no warnings/errors
it implements the logic needed 
however, certain printf statements and certain calcuations
are left for the OP

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


static const char * departmentNames[] =
{
    "Mens Department",
    "Womens Department",
    "Girls Department",
    "Boys Department"
};

int main()
{
    int  firstRecord = 1;
    FILE *in;

    char item[20];
    int departmentNum = 0;
    int quantity = 0;
    int prevDepartmentNum = 0;

    float cost    = 0.0f;  // store cost of one item
    float market  = 0.0f;  // customer cost of one item
    float cost2   = 0.0f;  // cost * quantity
    float mark2   = 0.0f;  // narket * quantity
    float totcost = 0.0f;  // sum of product cost for one department
    float totmark = 0.0f;  // sum of product sales for one department



    if((in = fopen("blinn.dat", "r")) == NULL)
    {
        perror( "fopen for reading blinn.data failed" );
        printf ("Can't open file blinn.dat\n");
        system("pause");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    // display the report header
    printf ("\n\n\t\t\t\t  Blinn Apparel Store");
    printf ("\n\n\n\t\t        Unit Cost                 Extended");
    printf ("\n             Quantity     Cost  Market         Cost      Market\n");


    while(1)
    {
        memset(item, 0x00, sizeof(item) );
        if( 5 != fscanf(in, " %d %19s %d %f %f", &departmentNum, item, &quantity, &cost, &market) )
        { // fscanf failed
            perror( "fscanf failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        if( firstRecord )
        {
            firstRecord = 0;
            prevDepartmentNum = departmentNum;
            totcost = 0.0f;
            totmark = 0.0f;
        } // end if

        if(departmentNum != prevDepartmentNum)
        { // then output previous department totals

            printf( "\n%s", departmentNames[prevDepartmentNum]);
            printf ("\n  Total\t\t\t\t            $%8.2f  $%8.2f", totcost, totmark );
            // re-initialize for new department
            totcost = 0.0f;
            totmark = 0.0f;
            prevDepartmentNum = departmentNum; // prep for next itteration
        } // end if

        cost2 = cost * quantity;
        mark2 = market * quantity;

        printf ("\n%8s       %4d   %8.2f   %4.2f       %8.2f   %8.2f   ",
                item, quantity, cost, market, cost2, mark2);
        totcost = totcost + cost2;
        totmark = totmark + mark2;
    } // end while

    system ("pause");
    return(0);
} // end function: main
0
Philip Kyes On

perhaps you could try using arrays like;

    int department[ k]
    string itemname[ ]
    Float quantity[ ] , cost_to_buy[ ],
    cost_to_sale[ ]

Ask the user for k number of departments if necessary for feasibility After this you can apply a switch or if statement to assign each array its equivalent and then display using a for-loop for each. With arrays you can work with larger number of departments Good luck