Home > Back-end >  Point-of-Sales in C Language
Point-of-Sales in C Language

Time:01-25

I am very new here so I still don't know how this site works but anyways, I need help!

I'm a total beginner in c programming (actually in programming) and I'm making a simple point-of-sales program. Here's the code I've made so far...

#include <stdio.h>

int code, qty, nxt;
float price, amt, total, cash, change; 
char next, ans;

void main()
{
do {
    system("cls");
    // Display all available products
    printf("     MARGIE'S SARI-SARI STORE");
    printf("\n\n      --AVAILABLE PRODUCTS-- ");
    printf("\n = = = = = = = = = = = = = = = = = \n\n");
    printf("[1] Eggs\t\tPhp 9.00\n");
    printf("[2] Noodles\t\tPhp 10.00\n");
    printf("[3] Sardine\t\tPhp 20.00\n");
    printf("\n = = = = = = = = = = = = = = = = = \n");

    do {
        printf("\nProduct\t\t: ");
        scanf("%d", &code);
        printf("Quantity\t: ");
        scanf("%d", &qty);

        switch (code)
        {
            case 1:
                price = 9.00;
            break;
            case 2:
                price = 10.00;
            break;
            case 3:
                price = 20.00;
            break;
            default: printf("Invalid input!"); break;
        }

        // Compute the amount for each product
        amt = price * qty;
        printf("Amount\t\t: %.2f", amt);

        // Compute total amount for all purchase
        total = total   amt;
        
        printf("\nNext? Enter any number (0 to exit): ");
        scanf("%d", &nxt);
    } while (nxt!=0);

    printf("\n = = = = = = = = = = = = = = = = = \n");
    printf("TOTAL\t\t: %.2f", total);
    printf("\n = = = = = = = = = = = = = = = = = \n");

    do {
        // Input cash payment, credit not allowed
        printf("\nCash\t\t: ");
        scanf("%f", &cash);

        // Compute amount change
        change = cash - total;
        printf("Change\t\t: %.2f", change);
    } while (cash<total);
    
    printf("\n\nNext Transaction? [y/n]: ");
    ans = getch();
    
} while (ans=='y' || ans=='Y');
}

Here is the output:

     MARGIE'S SARI-SARI STORE      

      --AVAILABLE PRODUCTS--       
 = = = = = = = = = = = = = = = = = 

[1] Eggs                Php 9.00   
[2] Noodles             Php 10.00  
[3] Sardine             Php 20.00  

 = = = = = = = = = = = = = = = = = 

Product         : 1
Quantity        : 1
Amount          : 9.00
Next? Enter any number (0 to exit): 0

 = = = = = = = = = = = = = = = = = 
TOTAL           : 9.00
 = = = = = = = = = = = = = = = = =

Cash            : 10
Change          : 1.00

Next Transaction? [y/n]: y     
     
     MARGIE'S SARI-SARI STORE

      --AVAILABLE PRODUCTS--
 = = = = = = = = = = = = = = = = =

[1] Eggs                Php 9.00
[2] Noodles             Php 10.00
[3] Sardine             Php 20.00

 = = = = = = = = = = = = = = = = =

Product         : 2
Quantity        : 2
Amount          : 20.00
Next? Enter any number (0 to exit): 1

Product         : 3
Quantity        : 2
Amount          : 40.00
Next? Enter any number (0 to exit): 0

 = = = = = = = = = = = = = = = = =
TOTAL           : 69.00
 = = = = = = = = = = = = = = = = =

Cash            : 100
Change          : 31.00

Next Transaction? [y/n]: n

As you can see, I'm having problem with the total because it adds the previous total computed from the previous transaction. How do I clear the previous computation in total so that if I want to run it again the previous total would not add to the new one? Please feel free to run this if you have a compiler at hand. Thankss

CodePudding user response:

Add total=0 after system("cls");

  •  Tags:  
  • Related