#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
uj5u.com熱心網友回復:
您不希望scanf()格式字串包含換行符。重構以減少代碼重復(當你澄清公式時我會修改我的答案):
#include <stdio.h>
struct option {
char *string;
float price;
};
float ask(const char *prompt, size_t n, const struct option options[n]) {
for(;;) {
printf("%s\n\n", prompt);
for(int i = 0; i < n; i ) {
printf("[%d] %s - $%.2f\n"
"-----------------------------------------------------\n",
i 1,
options[i].string,
options[i].price
);
}
printf("\n");
int o;
if(scanf("%d", &o) != 1) {
printf("Option could not be read.\n\n");
while(getchar() != '\n');
continue;
}
printf("\n");
for(int i = 0; i < n; i ) {
if(o == i 1)
return options[i].price;
}
printf("Option %d was not valid.\n", o);
}
}
int main(void) {
for(;;) {
float service = ask(
"-------------Welcome-------------\n"
"**********M.A.C PETS SPA*********\n"
" choose from our Specials\n"
"---------------------------------",
3,
(const struct option []) {
{ "CLEAN UP Special includes General shower and haircut", 20 },
{ "THE WORKS Special includes General shower, haircut, ear cleaning, and nail trim", 30
},
{ "FULL GROOM Special includes Breed appropriate shower, specific haircut, nail trim, ear cleaning, bandana and cologne", 40 }
}
);
float size = ask(
"What size is your dog?",
3,
(const struct option []) {
{ "Small", 0 },
{ "Medium", 5 },
{ "Large", 15 },
}
);
printf("Total Price including extra charge for the size is = $%.2f\n\n", service size);
}
}
和示例運行:
-------------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
-----------------------------------------------------
1
What size is your dog?
[1] Small - $0.00
-----------------------------------------------------
[2] Medium - $5.00
-----------------------------------------------------
[3] Large - $15.00
-----------------------------------------------------
x
Option could not be read.
What size is your dog?
[1] Small - $0.00
-----------------------------------------------------
[2] Medium - $5.00
-----------------------------------------------------
[3] Large - $15.00
-----------------------------------------------------
2
Total Price including extra charge for the size is = $25.00
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/534282.html
上一篇:動態集的陣列和回圈實作?
下一篇:通過while回圈添加整數輸入
