Need to improve this code.

Thank you, Dhayden. :D
Last edited on
When posting code, please put it in code tags so it's easier to refer to. Highlight the code and click the "<>" button to the right of the edit window.

This is C code, not C++, but I'll help you out anyway.

You need to store the number of items of each type on hand. To avoid repetition, it's a good idea to store the name of the items too. Let's create a class to store them together:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Item {
    int prod_left;              // number available
    const char *name;           // the name
};

// The inventory itself. NULL name indicates last one
Item inventory[] = {
    { 100, "Bottled Water" },
    { 100, "Bottled juice" },
    { 100, "Baked Foods" },
    { 100, "Foiled Foods" },
    { 100, "Medical Supplies" },
    { 100, "Liquor" },
    { 100, "Tetra milk"},
    { 100, "Tetra Chocolate" },
    { 100, "Other Services" },
    { 100, "Tobacco" },
    { 0, NULL}
};


You may need to store the total price of the items sold too.

There's a lot of repetition in your code and that's always a good candidate for a function. Here's a function that prompts for a sale and gets the product, quantity and price:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Get the production number, quantity and price of a sale.
// This decrements the quantity from the prod_left of the product
void getSale(int &prod, int &quantity, float &price)
{
    // Print the inventory code in two columns
    for (int i=0; inventory[i].name; ++i) {
        printf("%d -> %s", i+1, inventory[i].name);
        ++i;
        if (inventory[i].name == NULL) break;
        // print second column
        printf("\t%d -> %s\n", i+1, inventory[i].name);
    }
    printf("\nProduct Code: ");
    scanf("%d", &prod);
    // Convert from user-friendly numbers 1 to N, to programming
    // friendly numbers 0 to N-1
    --prod;
    printf("Quantity: ");
    scanf("%d", &quantity);
    printf("Price: ");
    scanf("%f", &price);
    inventory[prod].prod_left -= quantity;
}


There's also a lot of code that reports on the inventory. Here's a function to do this. I could be wrong, but I suspect that what you wanted to report was the number of items available, with a warning if the number is less than 70:
1
2
3
4
5
6
7
8
9
void reportInventory(int prod)
{
    if (inventory[prod].prod_left >= 70) {
        printf("\nProduct Left +> %d", inventory[prod].prod_left);
    } else {
        printf("\n\n!Product Shortage +> product left %d!",
               inventory[prod].prod_left);
    }
}


In your main function, you repeat the variables a lot. Also there are a lot of loops that don't appear to be needed. Here is the start of a version of main that uses the functions above and avoids the loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int
main()
{
    int trans, sum, i, e, count = 0, sales;
    int prod;
    int quantity;
    int bulk_disc;
    float price;        // unit price of item
    float total_sale;   // total price of item from
    float disc_price;   // discounted price
    float prod_left;    // product left

    printf
        ("WELCOME to PIT Stop!\n\n Guidline for user =+>\n\n\n Transaction\n\n\\
n 1 ->Discounted Transaction\n 2 ->Normal Transaction\n 3 ->Bulk Order Transact\
ion\n 4 ->INVENTORY\n 5 ->Record Sale");
    printf("\n\n Enter Transaction: ");

    scanf("%d", &trans);

    switch (trans) {
    case 1:
        printf("DISCOUNTED TRANSACTION\n");
        getSale(prod, quantity, price);

        total_sale = quantity * price;
        disc_price = total_sale - (total_sale * .20);
        printf("\nTotal Product Sold: %d", quantity);
        printf("\n\nDiscounted price = %.2f", disc_price);
        reportInventory(prod);
        break;

    case 2:
        printf("NORMAL TRANSACTION\n");
        getSale(prod, quantity, price);
        total_sale = quantity * price;

        printf("\nTotal Product Sold: %d", quantity);
        printf("\n\nTotal price = %.2f", total_sale);
        reportInventory(prod);
        break;

See if you can finish the program from here.
@dhayden,

Sorry 'cause I'm really a beginner and I'm not good enough to write my own code to make this project. But I'd gladly need your help in this, if I may ask for the whole new improvised code this..can you post it for me? We just studied string function recently and we were asked to make a program for our finals and test it. I could understand your explanations and points, but I can't seem to continue and finish this.. help me please?
Sorry, I won't write your code for you. I hope you understand.

The big change here is that you need to store the inventory for each item. When you make a sale, subtract the quantity sold from the inventory for that item.
 
void getSale(int &prod, int &quantity, float &price)


Or

 
void getSale(int prod, int quantity, float price)
void getSale(int &prod, int &quantity, float &price)
The parameters are passed by reference because the function sets them for use by the caller.


Topic archived. No new replies allowed.