0

I have been writing code for online shopping in C. In the code, if I select that I want to buy all 4 categories and the switch statement arrives in the Interiors and design Case, Here if I select the no of items I want as 1 the case is repeating itself. This does not happen if I select the no of items as 2 or 3 or 4 it is going to the next case that is is Fashion and design. And the Fashion and design case goes on continuously without terminating. I am not able to figure out the error so can you help me with it?

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

void main() {
    FILE *fpointer;
    char singleLine[1000];
    char name[100],ch;
    SYSTEMTIME stime;
    GetSystemTime(&stime);
    int choice, choose, num, *a, n, *b, i, j, m, z, *c, q, *d, t, p, *e;
    float quant;
    int quant1, quant2, g, quant3;
    float total;
    float total_e = 0;
    float total_g = 0;
    float total_f = 0;
    float total_i = 0;
    float total1 = 0;
    float total2 = 0;
    float total3 = 0;
    float total4 = 0;
    float total5 = 0;
    float total6 = 0;
    float total7 = 0;
    float total10 = 0;
    float total11 = 0;
    float total12 = 0;
    float total13 = 0;
    float total14 = 0;
    float total15 = 0;
    float total16 = 0;
    float total17 = 0;
    float total18 = 0;
    float total19 = 0;
    float total20 = 0;
    float total21 = 0;
    float total22 = 0;
    float total23 = 0;
    float total24 = 0;
    float total25 = 0;
    char str[1000];
    FILE *fp;
    fpointer = fopen("groceries1.txt\0", "r");
    printf("\n\n\nHistory of purchase is \n");
    while (!feof(fpointer)) {
        fgets(singleLine, 1000, fpointer);
        puts(singleLine);
    }
    fclose(fpointer);
    fp = fopen("groceries1.txt", "a");
    a = (int *)malloc(n * sizeof(int));
    b = (int *)malloc(n * sizeof(int));
    c = (int *)malloc(n * sizeof(int));
    d = (int *)malloc(n * sizeof(int));
    e = (int *)malloc(n * sizeof(int));
    printf("Enter your name \n");
    scanf("%s", name);
    printf("\nHello %s .We from KK Enterprises welcome you to Online Shopping \n.\n.\n.\n", name);
    printf("\nWhat type of product you are looking for \n");
    printf(" \n1.Groceries \n 2.Electronic Gadgets \n 3.Interiors/Households \n 4.Fashion \n");
    printf("\nEnter the number of categories under which you want to buy \n");
    scanf("%d", &n);
    printf("\nPlease enter the corresponding key of the category under which your purchase comes\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++) {
        switch (a[i]) {
        case 1:
            printf("Items available :-1.White Rice :- 50 per Kg \n"
                   " 2.Red Rice :- 40 per kg \n"
                   "3.Ashirwaad Wheat :- 50 per kg  \n"
                   "4.Surf Exel Powder :- 45 per kg  \n"
                   "5.Bathing Soap :- 50 per qnty  \n"
                   "6. Glass CLeaner :-200 per qnty\n"
                   "7.Room Freshner :- 400/qnty");
            printf("\n \nEnter the number of grocerry items you want \n");
            scanf("%d", &m);
            printf("\n\n Enter the corresponding key to buy the product \n");
            for (j = 0;j < m; j++) {
                scanf("%d", &b[j]);
            }
            for (j = 0;j < m; j++) {
                switch (b[j]) {
                case 1:
                    printf("Enter the quantity of White rice you want to add to the cart \n");
                    scanf("%f", &quant);
                    total1 = quant * 50;
                    printf("\n %f is the cost \n", total1);
                    fprintf(fp, " %f kgs of White rice , Cost Rs%f - %d / %d / %d\n",
                            quant, total1, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 2:
                    printf("Enter the quantity of Red rice you want to add to the cart \n");
                    scanf("%f", &quant);
                    total2 =  quant * 40;
                    printf("\n %f is the cost\n", total2);
                    fprintf(fp, " %f kgs of Red rice which cost Rs%f - %d / %d / %d\n",
                            quant, total2, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 3:
                    printf("Enter the quantity of Aashirwaad Wheat you want to add to the cart \n");
                    scanf("%f", &quant);
                    total3 = quant * 50;
                    printf("\n %f is the cost \n", total3);
                    fprintf(fp, " %f kgs of Aashirwad wheat which cost Rs%f - %d / %d / %d\n",
                            quant, total3, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 4:
                    printf("Enter the quantity of Surf Exel Powder you want to add to the cart \n");
                    scanf("%f", &quant);
                    total4 =  quant * 45;
                    printf("\n %f is the cost\n", total4);
                    fprintf(fp, " %f kgs of Surf Exel which cost Rs%f - %d / %d / %d\n",
                            quant, total4, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 5:
                    printf("Enter the number of Bathing Soaps you want add to the cart \n");
                    scanf("%d", &num);
                    total5 =  num * 50;
                    printf("\n %f is the cost\n", total5);
                    fprintf(fp, " %d  of Bathing Soap which cost Rs%f - %d / %d / %d\n",
                            num, total5, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 6:
                    printf("Enter the number of Glass Cleaning Liquid you want add to the cart \n");
                    scanf("%d", &num);
                    total6 =  num * 200;
                    printf("\n %f is the cost \n", total6);
                    fprintf(fp, " %d  of Glass Cleaning Liquid which cost Rs%f - %d / %d / %d\n",
                            num, total6, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 7:
                    printf("Enter the number of Room Freshner Spray you want add to the cart \n");
                    scanf("%d", &num);
                    total7 =   num * 400;
                    printf("\n %f is the cost \n", total7);
                    fprintf(fp, " %d  of Room Freshner which cost Rs%f - %d / %d / %d\n",
                            num, total7, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                default:
                    printf("Invalid choice \n");

                }

            }
            total_g = total1 + total2 + total3 + total4 + total5 + total6 + total7;
            printf(" \n\n Total of groceries is %f \n", total_g);
            break;
        case 2:
            printf("Electronic Gadgets available are \n");
            printf("1.One plus 8T  Rs 45,000 \n");
            printf("2.IPHONE 12 Rs 1,00,000 \n");
            printf("3.Samsung Galaxy M20 Rs 20,000\n");
            printf("4.Samsung 8K Display 64 inch T.V Rs 4,00,000\n");
            printf("5.Sony Bravia 8K Display 64 inch Rs 4,00,000\n");
            printf("Enter the number of items you want out of the list above\n");
            scanf("%d", &z);
            printf("Enter the corresponding key/keys of the products you want\n");
            for (t = 0; t < z; t++) {
                scanf("%d", &c[t]);
            }
            for (t = 0; t < z; t++) {
                switch (c[t]) {
                case 1:
                    printf("Enter the number of One Plus 8T smart Phones you want\n");
                    scanf("%d", &quant1);
                    total10 = quant1 * 45000;
                    printf("Cost is %f \n", total10);
                    fprintf(fp, " %d  One Plus 8T which cost Rs%f - %d / %d / %d\n",
                            quant1, total10, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 2:
                    printf("Enter the number of IPHONE 12 smart Phones you want\n");
                    scanf("%d", &quant1);
                    total11 = quant1 * 100000;
                    printf("Cost is %f \n", total11);
                    fprintf(fp, " %d  IPHONE 12 which cost Rs%f - %d / %d / %d\n",
                            quant1, total11, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 3:
                    printf("Enter the number of Samsung Galaxy M20 smart Phones you want\n");
                    scanf("%d", &quant1);
                    total12 = quant1 * 20000;
                    printf("Cost is %f \n", total12);
                    fprintf(fp, " %d  Samsung Galaxy M20 which cost Rs%f - %d / %d / %d\n",
                            quant1, total12, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 4:
                    printf("Enter the number of Samsung 8K Display 64 inch T.V you want\n");
                    scanf("%d", &quant1);
                    total13 = quant1 * 400000;
                    printf("Cost is %f \n", total13);
                    fprintf(fp, " %d  Samsung 8K Display 64 inch which cost Rs%f - %d / %d / %d\n",
                            quant1, total13, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 5:
                    printf("Enter the number of Sony Bravia 8K Display 64 inch T.V you want\n");
                    scanf("%d", &quant1);
                    total14 = quant1 * 400000;
                    printf("Cost is %f \n", total14);
                    fprintf(fp, " %d  Sony Bravia 8K Display 64 inch which cost Rs%f - %d / %d / %d\n",
                            quant1, total14, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                default:
                    printf("Invalid Argument \n");
                }
            }
            total_e = total10 + total11 + total12 + total13 + total14;
            printf("Total cost of electronics equipments is %f \n", total_e);
            break;
        case 3:
            printf("1.Curtains Rs 4000\n"
                   " 2.Sofa Set Rs 15,000 \n"
                   " 3.Bed Rs 20,000 \n"
                   " 4.Pillows Rs 3,000\n"
                   " 5.Chairs Rs 15,000\n"
                   " 6.Tables Rs 10,000\n");
            printf("Enter the number of items from the list you want \n");
            scanf("%d", &p);
            printf("Enter the corresponding key/keys of the items you want to buy \n");
            for (i = 0; i < p; i++) {
                scanf("%d", &d[i]);
            }
            for (i = 0; i < p; i++) {
                switch (d[i]) {
                case 1:
                    printf("Enter the number of curtain sets you want to buy \n");
                    scanf("%d", &quant2);
                    total15 = quant2 * 4000;
                    printf("Cost is %f \n", total15);
                    fprintf(fp, " %d Curtain sets which cost Rs%f - %d / %d / %d\n",
                            quant2, total15, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 2:
                    printf("Enter the number of Sofa sets you want to buy \n");
                    scanf("%d", &quant2);
                    total16 = quant2 * 15000;
                    printf("Cost is %f \n", total16);
                    fprintf(fp, " %d Sofa set which cost Rs%f - %d / %d / %d\n",
                            quant2, total16, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 3:
                    printf("Enter the number of Bed sets you want to buy \n");
                    scanf("%d", &quant2);
                    total17 = quant2 * 20000;
                    printf("Cost is %f \n", total17);
                    fprintf(fp, " %d Bed set which cost Rs%f - %d / %d / %d\n",
                            quant2, total17, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 4:
                    printf("Enter the number of Pillows you want to buy \n");
                    scanf("%d", &quant2);
                    total18 = quant2 * 3000;
                    printf("Cost is %f \n", total18);
                    fprintf(fp, " %d Pillows which cost Rs%f - %d / %d / %d\n",
                            quant2, total18, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 5:
                    printf("Enter the number of chairs you want to buy \n");
                    scanf("%d", &quant2);
                    total19 = quant2 * 15000;
                    printf("Cost is %f \n", total19);
                    fprintf(fp, " %d Chairs which cost Rs%f - %d / %d / %d\n",
                            quant2, total19, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 6:
                    printf("Enter the number of tables you want to buy \n");
                    scanf("%d", &quant2);
                    total20= quant2 * 10000;
                    printf("Cost is %f \n", total20);
                    fprintf(fp, " %d Tables which cost Rs%f - %d / %d / %d\n",
                            quant2, total20, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                default:
                    printf("Invalid argument \n");
                }
            }
            total_f = total15 + total16 + total17 + total18 + total19 + total20;
            printf("The total cost of Interiors and Households is %f \n", total_f);
            break;
        case 4:
            printf("1.T-Shirt Rs 500 \n"
                   " 2.Shoes Rs 600 \n"
                   " 3.Pants Rs 700 \n"
                   "4.Frocks Rs 800\n"
                   " 5.Sunglases Rs 200\n");
            printf("Enter the no of items u want to purchase from the above list \n");
            scanf("%d", &g);
            printf("Enter the corresponding key/keys to purchase the item \n");
            for (i = 0; i < g; i++) {
                scanf("%d", &e[i]);
            }
            for (i = 0; i < g; i++) {
                switch (e[i]) {
                case 1:
                    printf("Enter the number of T-shirts you want to buy \n");
                    scanf("%d", &quant3);
                    total21 = quant3 * 500;
                    printf("Cost is %f\n", total21);
                    fprintf(fp, " %d T-shirts which cost Rs%f - %d / %d / %d\n",
                            quant3, total21, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 2:
                    printf("Enter the number of Shoes you want to buy \n");
                    scanf("%d", &quant3);
                    total22 = quant3 * 600;
                    printf("Cost is %f\n", total22);
                    fprintf(fp, " %d Shoes which cost Rs%f - %d / %d / %d\n",
                            quant3, total22, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 3:
                    printf("Enter the number of Pants you want to buy \n");
                    scanf("%d", &quant3);
                    total23 = quant3 * 700;
                    printf("Cost is %f\n", total23);
                    fprintf(fp, " %d Pants which cost Rs%f - %d / %d / %d\n",
                            quant3, total23, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 4:
                    printf("Enter the number of Frocks you want to buy \n");
                    scanf("%d", &quant3);
                    total24 = quant3 * 800;
                    printf("Cost is %f\n", total24);
                    fprintf(fp, " %d Frocks which cost Rs%f - %d / %d / %d\n",
                            quant3, total24, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                case 5:
                    printf("Enter the number of Sunglases you want to buy \n");
                    scanf("%d", &quant3);
                    total25 = quant3 * 200;
                    printf("Cost is %f\n", total25);
                    fprintf(fp, " %d Sunglases which cost Rs%f -2 %d / %d / %d\n",
                            quant3, total25, stime.wDay, stime.wMonth, stime.wYear);
                    break;
                default:
                    printf("Invalid argument\n");
                }
            }
            total_i = total21 + total22 + total23 + total24 + total25;
            printf("Total cost of Fashion/design items are %f", total_i);
            break;
        default:
            printf("Invalid choice\n");
        }
    }
    total = total_g + total_f + total_e + total_i;
    printf("Your Bill is %f \n", total);
    fclose(fp);
}
7
  • 3
    I think you need to learn about arrays and structs Dec 11, 2020 at 10:20
  • 2
    im sorry to say but the code seems bad not only that but you don't use any tabs or spaces its really hard seeing what even i am looking at
    – Imeguras
    Dec 11, 2020 at 10:22
  • fpointer = fopen("groceries1.txt\0","r"); the \0 is unecessary the compiler already does the \0 for you
    – Imeguras
    Dec 11, 2020 at 10:24
  • 2
    Also its time for learning functions , styles and indentation
    – IrAM
    Dec 11, 2020 at 10:32
  • 1
    For a MCVE I assume you could have removed ~80% of the code without losing any informattion.
    – Gerhardh
    Dec 11, 2020 at 10:48

1 Answer 1

1

The thing you asked is a very complex answer so ill slowly update the code as i edit it and correct it there was already a missing break in your switch

Okay you really should use functions like it would cut on 50% of your variables im still at 25% of your code

I would advise you to maybe use a better IDE, especially one that comes with snippets and indents and formats your text for you since your code was so long and uninterrupted that it was hard for me to fix it https://p.teknik.io/7p1eD ok here is your code kinda cleaned i might have change some things but i placed some comment to warn you

and here is mine this is long from ending and ill slowly build a response so feel free to ask me anything that you dont understand

#include <stdio.h>
#include <stdlib.h>
//#include<conio.h>
//#include<windows.h>
#define QUANTITY_MESSAGE_INTEGER "Enter the number of %s you want to add to the cart \n"
#define QUANTITY_MESSAGE_FLOAT "Enter the quantity of %s you want to add to the cart \n"
double charge(char const * message, char * name, double price);
double readNumber(double min, double max);
void cleanBufferStdin(void);
void main()
{
    FILE *fpointer;
    char singleLine[1000];
    char name[100],ch;
    SYSTEMTIME stime;
    GetSystemTime(&stime);
    int choice,choose,num,*a,n,*b,i,j,m,z,*c,q,*d,t,p,*e;
    float quant;
    int quant1,quant2,quant3,g;
    double biggertotal, total;
    char str[1000];
    FILE *fp;
    fpointer = fopen("groceries1.txt","r");
    printf("\n\n\nHistory of purchase is \n");
    //WHAT? why the fgets
    while(!feof(fpointer)){
        fgets(singleLine,1000,fpointer);
        puts(singleLine);
    }
    fclose(fpointer);

    
    //n doesnt isnt assigned any value this can be dangerous as it can output random errors to the screen
    a=(int*)malloc(n*sizeof(int));
    b=(int*)malloc(n*sizeof(int));
    c=(int*)malloc(n*sizeof(int));
    d=(int*)malloc(n*sizeof(int));
    e=(int*)malloc(n*sizeof(int));
    printf("Enter your name \n");
    //there is no validation here
    scanf("%s",name);
    printf("\nHello %s .We from KK Enterprises welcome you to Online Shopping \n.\n.\n.\n",name);
    printf("\nWhat type of product you are looking for \n");
    printf(" \n1.Groceries \n 2.Electronic Gadgets \n 3.Interiors/Households \n 4.Fashion \n");
    printf("\nEnter the number of categories under which you want to buy \n");
    //wait WHAT?
    scanf("%d",&n);
    printf("\nPlease enter the corresponding key of the category under which your purchase comes\n");
    //Why
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++){
        total=0;
        switch(a[i]){
            case 1 :
                printf("Items available :-1.White Rice :- 50 per Kg \n 2.Red Rice :- 40 per kg \n3.Ashirwaad Wheat :- 50 per kg  \n4.Surf Exel Powder :- 45 per kg  \n5.Bathing Soap :- 50 per qnty  \n6. Glass CLeaner :-200 per qnty\n7.Room Freshner :- 400/qnty");
                printf("\n \nEnter the number of grocerry items you want \n");
                scanf("%d",&m);
                printf("\n\n Enter the corresponding key to buy the product \n");
                for(j=0;j<m;j++){
                    scanf("%d",&b[j]);
                }
                for(j=0;j<m;j++){
                    switch(b[j]){
                        case 1:
                            total+=charge(QUANTITY_MESSAGE_FLOAT, "White rice",50);
                        break;
                        case 2:
                            total+=charge(QUANTITY_MESSAGE_FLOAT, "Red rice",40);
                        break;
                        case 3: 
                            total+=charge(QUANTITY_MESSAGE_FLOAT, "Aashirwaad Wheat",50);
                        break;
                        case 4: 
                            total+=charge(QUANTITY_MESSAGE_FLOAT, "Surf Exel Powder",45);
                        break;
                        case 5:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Bathing Soaps",50);
                            
                        break;
                        case 6:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Glass Cleaning Liquid",200);
                        break;
                        case 7: 
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Room Freshner Spray",400);
                        break;
                        default:
                            printf("Invalid choice \n");
                        break;
                    }  
                }
                biggertotal+=total;
                printf(" \n\n Total of groceries is %f \n",total);
                break;
            case 2:
                printf("Electronic Gadgets available are \n");
                printf("1.One plus 8T  Rs 45,000 \n");
                printf("2.IPHONE 12 Rs 1,00,000 \n");
                printf("3.Samsung Galaxy M20 Rs 20,000\n");
                printf("4.Samsung 8K Display 64 inch T.V Rs 4,00,000\n");
                printf("5.Sony Bravia 8K Display 64 inch Rs 4,00,000\n");
                printf("Enter the number of items you want out of the list above\n");
                scanf("%d",&z);
                printf("Enter the corresponding key/keys of the products you want\n");
                for(t=0;t<z;t++){
                    scanf("%d",&c[t]);
                }
                for(t=0;t<z;t++){
                    switch(c[t]){
                        case 1:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, " One Plus 8T smart Phones",45000);
                        break;
                        case 2:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "IPHONE 12 smart Phones",100000);
                        break;
                        case 3:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Samsung Galaxy M20 smart Phones",20000);
                        break;
                        case 4:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Samsung 8K Display 64 inch T.V",400000);
                        break;
                        case 5:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Sony Bravia 8K Display 64 inch T.V",400000);

                        break;
                        default:
                            printf("Invalid Argument \n");
                        //Another break missing here
                            break;
                    }
                }
            biggertotal+=total;
            printf("Total cost of electronics equipments is %f \n",total);
            break;
            case 3:
                printf("1.Curtains Rs 4000\n 2.Sofa Set Rs 15,000 \n 3.Bed Rs 20,000 \n 4.Pillows Rs 3,000\n 5.Chairs Rs 15,000\n 6.Tables Rs 10,000\n");
                printf("Enter the number of items from the list you want \n");
                scanf("%d",&p);
                printf("Enter the corresponding key/keys of the items you want to buy \n");
                for(i=0;i<p;i++){
                    scanf("%d",&d[i]);
                }
                for(i=0;i<p;i++){
                    switch(d[i]){
                        case 1:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Curtain",4000);
                        break;
                        case 2: 
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Sofa", 15000);
                        break;
                        case 3:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Bed sets",20000);
                        break;
                        case 4:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Pillows",3000);
                        break;
                        case 5:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Chairs", 15000);
                        break;
                        case 6:
                            total+=charge(QUANTITY_MESSAGE_INTEGER, "Tables", 10000);
                        break;
                        default:
                            printf("Invalid argument \n");
                        //NO break here again, this means that you dont know there should be a break after a default:
                        break;
                        }
                    }
                    biggertotal+=total;
                    printf("The total cost of Interiors and Households is %f \n",total);
                    break;
                    case 4:
                        printf("1.T-Shirt Rs 500 \n 2.Shoes Rs 600 \n 3.Pants Rs 700 \n4.Frocks Rs 800\n 5.Sunglases Rs 200\n");
                        printf("Enter the no of items u want to purchase from the above list \n");
                        scanf("%d",&g);
                        printf("Enter the corresponding key/keys to purchase the item \n");
                        for(i=0;i<g;i++){
                            scanf("%d",&e[i]);
                        }
                        for(i=0;i<g;i++){
                            switch(e[i]){
                                case 1:
                                    total+=charge(QUANTITY_MESSAGE_INTEGER, "T-shirts",500);
                                break;
                                case 2:
                                    total+=charge(QUANTITY_MESSAGE_INTEGER, "Shoes", 600);
                                break;
                                case 3:
                                    total+=charge(QUANTITY_MESSAGE_INTEGER, "Pants",700);
                                break;
                                case 4:
                                    total+=charge(QUANTITY_MESSAGE_INTEGER,"Frocks", 800);
                                break;
                                case 5: 
                                    total+=charge(QUANTITY_MESSAGE_INTEGER,"Sunglases", 200);   
                                break;
                                default:
                                    printf("Invalid argument\n");
                                break;
                            }
                        }
                        biggertotal+=total;
                        printf("Total cost of Fashion/design items are %f",biggertotal);
                    break;
                    default:
                        printf("Invalid choice\n");
                    break;
        }
    }
   
    printf("Your Bill is %f \n",biggertotal);
    fclose(fp);
}

double charge(char const * message, char * name, double price){
    FILE *fp; 
    fp=fopen("groceries1.txt", "a");
    double total;
    double quantity=0;
    printf(message, name);
    quantity=readNumber(0,9999);
    total= quantity*price; 
    printf("Cost is %f\n", total);
    if ((float)(quantity-(int)quantity) !=0)
    {
        fprintf(fp," %f kgs of %s which cost Rs%f - %d / %d / %d\n",quantity,name,total,stime.wDay,stime.wMonth,stime.wYear);
        //TODO Some check
    }else{
       fprintf(fp," %f units of %s which cost Rs%f - %d / %d / %d\n",quantity,name,total,stime.wDay,stime.wMonth,stime.wYear);
        //TODO Some check 
    }
    fclose(fp);
    
    
    return total; 
}
double readNumber(double min, double max){
    double num, control;
    do{
        control = scanf ("%f", &num);
        cleanBufferStdin();  
        if (control == 0)
        {
            printf("You should enter a number \n");
        }
        else{
            if(num<min || num>max)
            {
                printf("Number is invalid.\n");
            }
        }
    }  
    while(num<min || num>max || control ==0);

    return num;
}
void cleanBufferStdin(void)
{
    char chr;
    do
    {
        chr = getchar();
    }
    while (chr != '\n' && chr != EOF);
}


i now realise how tedious to fix everything and i kinda admire how OP managed to be so passionate about C to write a so in depth home project(i can only assume) and how much time it will require to fix everything so if i take various days its because i got bored or have college to do but ill keep regular updates till i see a checkmark

Ok i think this is the last update for today, ill keep working in the answer tomorrow I dont think this can compile per say both because im focusing in optimizing the code and cutting lines aggressively instead of it working making something

3
  • Your code shows an error at line 65 stating too few arguments for function Chagrge??.So can help me out. Dec 14, 2020 at 12:11
  • although your question is somewhat easy to resolve, your code is ultra unorganized so im kinda organizing it for you and eventually the problem will disappear @KarthikKKumaraswamy
    – Imeguras
    Dec 16, 2020 at 10:15
  • Update i haven't finished my answer but it took so long that i stopped midway if anyone still needs an answer feel free to comment
    – Imeguras
    Oct 11, 2021 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.