#include <stdio.h>
#define beet 2.05
#define artichokes 1.15
#define carrot 1.09
int main()
{
int i,k=0;
double total_price_goods, discount=0, freight_packingExpense, total_order_cost, beet_pound=0, artichokes_pound=0, carrot_pound=0;
double total_price_beet, total_price_artichokes, total_price_carrots, a, b, c,total_pound;
char ch;
printf("ABC郵購雜貨店訂貨系統\n");
printf("a)洋薊 b)甜菜 c)胡蘿卜 q)退出\n");
while (1)
{
if (k != 1)
printf("請輸入你要購買貨物的編號:");
else
k = 0;
scanf_s("%c",&ch,1);
switch (ch)
{
case 'a':
printf("請輸入購買洋薊的磅數:");
scanf_s("%lf", &a);
artichokes_pound += a;
break;
case 'b':
printf("請輸入購買甜菜的磅數:");
scanf_s("%lf", &b);
beet_pound += b;
break;
case 'c':
printf("請輸入購買胡蘿卜的磅數:");
scanf_s("%lf", &c);
carrot_pound += c;
break;
case 'q':
break;
default :
printf("請輸入正確的編號:\n");
k = 1;
continue;
}
if (ch == 'q')
break;
}
total_price_artichokes = artichokes_pound * artichokes;
total_price_beet = beet * beet_pound;
total_price_carrots = carrot_pound * carrot;
total_price_goods = total_price_artichokes + total_price_beet + total_price_carrots;
if (total_price_goods >= 100)
discount = total_price_goods * 0.05;
total_pound = beet_pound + artichokes_pound + carrot_pound;
if (total_pound ==0)
freight_packingExpense = 0;
else
if (total_pound <= 5)
freight_packingExpense = 6.5;
else if (total_pound <= 20)
freight_packingExpense = 14;
else
freight_packingExpense = 14 + (total_pound - 20) * 0.5;
total_order_cost = total_price_goods + freight_packingExpense - discount;
for (i = 1;i <= 37;i++)
printf("*");
printf("訂單");
for (i = 1;i < 37;i++)
printf("*");
printf("\n");
printf("物品種類 物品售價 訂購重量(單位:磅) 物品費用\n");
printf(" a洋薊 %.2f %.2f %.2f \n ",artichokes,artichokes_pound,total_price_artichokes);
printf(" b甜菜 %.2f %.2f %.2f \n", beet, beet_pound, total_price_beet);
printf(" c胡蘿卜 %.2f %.2f %.2f \n", carrot, carrot_pound, total_price_carrots);
printf("訂單總費用:%.2f 折扣%.2f 運費和包裝費%.2f 費用總額:%.2f \n ",total_price_goods,discount,freight_packingExpense,total_order_cost);
for (i = 1;i < 80;i++)
printf("*");
printf("\n");
return 0;
}

圖片第一處為什么會有輸入正確編號,第二處為什么有兩個輸入正確編號求解??,新手剛學c語言1個月
uj5u.com熱心網友回復:
其實這個問題理一下邏輯就出來了。從你輸入10開始吧。這時候的switch結構已經退出,然后if (ch == 'q')這句話不成立,所以while回圈沒有退出。又開始回圈。因為你的k的值沒有變過,所以執行了printf("請輸入你要購買貨物的編號:");這句話。scanf_s("%c",&ch,1);接下來這句話又獲取了一個輸入,此時ch的值肯定不是a, b, c了,所以進入了default:執行了printf("請輸入正確的編號:\n");這句話。最后你輸入了d,又是default的情況。
uj5u.com熱心網友回復:
沒有手動除錯,scanf_s("%c",&ch,1);這句話第二次獲取的輸入應該是你輸入時候的回車。建議打斷點除錯一下uj5u.com熱心網友回復:
在用%c獲取有效輸入之前,需要先仔細地把輸入緩沖區打掃干凈。一般用while(getchar()!='\n');
就行。
uj5u.com熱心網友回復:
為什么此時的值肯定不是a.b.c
uj5u.com熱心網友回復:
還沒學到打斷點
uj5u.com熱心網友回復:
可以學著除錯代碼。因為你輸入一個字符過后,你還敲了一個回車。這也是一個字符。所以第二次scanf_s的時候可能就是讀取了回車這個字符。具體沒除錯也不敢肯定,但是既然又scanf_s了一次,你ch的值肯定是變了的
uj5u.com熱心網友回復:
在每個最后不帶\n的printf后面加fflush(stdout);
在每個不想受接識訓沖區舊內容影響的scanf前面加rewind(stdin);
另外請檢查scanf的回傳值。
//請今后要用
char c;
scanf("%c",&c);
//時,都改為
char s[2];
char c;
scanf("%1s",s);
c=s[0];
//自動跳過一個或多個空格或Tab字符或回車換行,讀取下一個字符。
按趙老師的經驗修改如下,供參考:
#include <stdio.h>
#define beet 2.05
#define artichokes 1.15
#define carrot 1.09
int main()
{
int i,k=0;
char s[2];
double total_price_goods, discount=0, freight_packingExpense, total_order_cost, beet_pound=0, artichokes_pound=0, carrot_pound=0;
double total_price_beet, total_price_artichokes, total_price_carrots, a, b, c,total_pound;
char ch;
printf("ABC郵購雜貨店訂貨系統\n");
printf("a)洋薊 b)甜菜 c)胡蘿卜 q)退出\n");
while (1)
{
if (k != 1)
{printf("請輸入你要購買貨物的編號:");
fflush(stdout);}
else
k = 0;
rewind(stdin);
scanf("%1s",s);//scanf_s("%c",&ch,1);
ch = s[0];
switch (ch)
{
case 'a':
printf("請輸入購買洋薊的磅數:");
fflush(stdout);
rewind(stdin);
scanf("%lf", &a);//scanf_s("%lf", &a);
artichokes_pound += a;
break;
case 'b':
printf("請輸入購買甜菜的磅數:");
fflush(stdout);
rewind(stdin);
scanf("%lf", &b);//scanf_s("%lf", &b);
beet_pound += b;
break;
case 'c':
printf("請輸入購買胡蘿卜的磅數:");
fflush(stdout);
rewind(stdin);
scanf("%lf", &c);//scanf_s("%lf", &c);
carrot_pound += c;
break;
case 'q':
break;
default :
printf("請輸入正確的編號:\n");
k = 1;
//continue;
}
if (ch == 'q')
break;
}
total_price_artichokes = artichokes_pound * artichokes;
total_price_beet = beet * beet_pound;
total_price_carrots = carrot_pound * carrot;
total_price_goods = total_price_artichokes + total_price_beet + total_price_carrots;
if (total_price_goods >= 100)
discount = total_price_goods * 0.05;
total_pound = beet_pound + artichokes_pound + carrot_pound;
if (total_pound ==0)
freight_packingExpense = 0;
else
if (total_pound <= 5)
freight_packingExpense = 6.5;
else if (total_pound <= 20)
freight_packingExpense = 14;
else
freight_packingExpense = 14 + (total_pound - 20) * 0.5;
total_order_cost = total_price_goods + freight_packingExpense - discount;
for (i = 1;i <= 37;i++)
printf("*");
printf("訂單");
for (i = 1;i < 37;i++)
printf("*");
printf("\n");
printf("物品種類 物品售價 訂購重量(單位:磅) 物品費用\n");
printf(" a洋薊 %.2f %.2f %.2f \n ",artichokes,artichokes_pound,total_price_artichokes);
printf(" b甜菜 %.2f %.2f %.2f \n", beet, beet_pound, total_price_beet);
printf(" c胡蘿卜 %.2f %.2f %.2f \n", carrot, carrot_pound, total_price_carrots);
printf("訂單總費用:%.2f 折扣%.2f 運費和包裝費%.2f 費用總額:%.2f \n ",total_price_goods,discount,freight_packingExpense,total_order_cost);
for (i = 1;i < 80;i++)
printf("*");
printf("\n");
return 0;
}
運行效果:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245438.html
標籤:C語言
上一篇:怎么修改檔案里的資訊啊?
下一篇:求求大佬幫我看看為什么運行不了
