繁体   English   中英

switch 语句中的重复案例

[英]Repeating cases in switch statement

我一直在 C 中编写在线购物代码。 在代码中,如果我想购买所有4类别的 select 并且switch语句到达Interiors and design Case ,这里如果我 select 我想要的项目数量为1案例正在重复 如果我 select 项目数量为234 ,则不会发生这种情况,它将进入下一个案例,即时尚和设计 时尚和设计案例不断进行,没有终止。 我无法找出错误,所以你能帮我解决吗?

这是我的代码:

#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);
}

您问的问题是一个非常复杂的答案,所以在我编辑并更正代码时会慢慢更新代码,您的开关中已经缺少中断

好吧,你真的应该使用这样的函数,因为它会减少 50% 的变量,我仍然是代码的 25%

我建议您使用更好的 IDE,尤其是带有片段和缩进并为您格式化文本的代码,因为您的代码太长且不间断,我很难修复它https://p.teknik。 io/7p1eD好的,你的代码有点干净,我可能已经改变了一些东西,但我发表了一些评论来警告你

这是我的,这距离结束还有很长的路要走,我会慢慢地做出回应,所以请随时问我任何你不明白的事情

#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);
}


我现在意识到修复所有事情是多么乏味,我有点钦佩 OP 如何对 C 如此热情地编写一个如此深入的家庭项目(我只能假设)以及修复所有事情需要多少时间所以如果我采取各种天是因为我很无聊或有大学要做,但我会定期更新,直到我看到一个复选标记

好的,我认为这是今天的最后一次更新,明天我会继续努力回答我不认为这可以编译,因为我专注于优化代码和积极地切割线,而不是让它工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM