【问题标题】:PETS SPA how to add the options for extra charge when the user picks small medium or largePETS SPA在用户选择小中或大时如何添加额外收费的选项
【发布时间】:2022-11-12 05:37:29
【问题描述】:
#include <stdio.h>

int main(void) {

int option;
int many;
char name;
float CNP = 20.00;

float TWS = 30.00;

float FG = 40.00;

float Many1 = 0.00;
float Many2 = 5.00;
float Many3 = 15.00;



while(1){

printf("-------------Welcome-------------\n");
printf("**********M.A.C PETS SPA*********\n");
printf("     choose from our Specials\n");
printf("---------------------------------\n");

printf("[1] CLEAN UP Special includes General shower and haircut - $20.00\n");
printf("-----------------------------------------------------\n");

printf("[2] THE WORKS Special includes General shower, haircut, ear cleaning, and nail trim - $30.00\n");
printf("-----------------------------------------------------\n");

printf("[3] FULL GROOM Special includes Breed appropriate shower, specific haircut, nail trim, ear cleaning, bandana and cologne - $40.00\n");
printf("-----------------------------------------------------\n");



printf("Enter your special: number:\n");
scanf("%d",&option);



if(option == 1)

{

printf("What size is your dog?: ");
printf("[1]: small\n");
printf("[2]: medium\n");
printf("[3]: Large\n");
scanf("%d\n",&many);

printf("Total Price including extra charge for the size is = $%.2f\n",(CNP+many) );

break;

}

else if(option == 2)

{

printf("What size is your dog?: \n");
printf("[1]: small\n");
printf("[2]: medium\n");
printf("[3]: Large\n");
scanf("%d",&many);

printf("Total Price including extra charge for the size is = $%.2f",TWS*many + (TWS*many*0.07) );

break;

}

else if(option == 3)  

{

printf("What size is your dog?: \n");
printf("[1]: small\n");
printf("[2]: medium\n");
printf("[3]: Large\n");


scanf("%d",&many);

printf("Total Price including extra charge for the size is = $%.2f",FG*many + (FG*many*0.07) );

break;

}



else printf("Invalid item number! Re-enter item number\n");

}

return 0;

}

我试图让用户选择他们的狗的大小,并根据他们选择的大小添加额外费用。我似乎做错了我觉得我需要创建另一个循环或使用结构我需要帮助请!!!

我尝试使用循环,输入可以说是他们选择第一个选项,大小为中等

那么输出将是 20.00 加上 15.00 = 35.00

【问题讨论】:

标签: c loops if-statement while-loop menu


【解决方案1】:

您不希望 scanf() 格式字符串包含换行符。重构以减少代码重复(当你澄清公式时,我会修改我的答案):

#include <stdio.h>

int main(void) {
    for(;;) {
        printf(
            "-------------Welcome-------------
"
            "**********M.A.C PETS SPA*********
"
            "     choose from our Specials
"
            "---------------------------------
"
            "[1] CLEAN UP Special includes General shower and haircut - $20.00
"
            "-----------------------------------------------------
"
            "[2] THE WORKS Special includes General shower, haircut, ear cleaning, and nail trim - $30.00
"
            "-----------------------------------------------------
"
            "[3] FULL GROOM Special includes Breed appropriate shower, specific haircut, nail trim, ear cleaning, bandana and cologne - $40.00
"
            "-----------------------------------------------------
"
        );

        int option;
        printf("Enter your special: number:
");
        if(scanf("%d", &option) != 1 || option < 1 || option > 3) {
            printf("Invalid option");
            continue;
        }
    
        int many;
        printf(
            "What size is your dog?:
"
            "[1]: small
"
            "[2]: medium
"
            "[3]: Large
"
        );
        if(scanf("%d", &many) != 1 || many < 1 || many > 3) {
            printf("Invalid size
");
            continue;
        }

        float services[] = {
            20,
            30,
            40
        };
        float sizes[] = {
            0,
            5,
            15
        };
        float total;
        if(option == 1)
            total = services[option - 1] + sizes[many - 1];
        else if(option == 2)
            total = 1.07 * services[option - 1] * sizes[many - 1];
        else if(option == 3)
            total = 1.07 * services[option - 1] * sizes[many - 1];
        printf("Total Price including extra charge for the size is = $%.2f
", total);
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-05
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多