我正在研究這個程序,要求用戶輸入電路半徑,然後要求他們選擇他想要計算的:面積,周長,或汽缸容積..'計算電路的面積和周長'程序在c ...奇怪的輸出
#include <stdio.h>
int main(void)
{
float radius=0.0,height=0.0;
char choice,quit;
const float pi=3.1415658;
do
{
printf("Enter radius : \n");
scanf("%f",&radius);
printf("\n\nWhat do you want to calc.?\nArea of circuit --> press A\n"
"Circumference of circuit --> press C\nVolume of cylinder --> press V\nQuit --> Q\n");
scanf("%c",&choice);
switch (choice)
{
case 'A':
printf("Area = %.5f\n",radius*radius*pi);
break;
case 'C':
printf("Circumference = %.5f\n",2*radius*pi);
break;
case 'V':
printf("Enter Hight : \n");
scanf("%f",&height);
printf("Volume of cylinder = %.5f\n",radius*radius*pi*height);
break;
case 'Q':
quit='y';
break;
default:
printf("default!!\n");
break;
}
}while(quit != 'y');
return 0;
}
但是當我運行該程序
Enter radius : 3 What do you want to calc.? Area of circuit --> press A Circumference of circuit --> press C Volume of cylinder --> press V Quit --> Q /*here before I choose anything the next line appears, skipping reading the choice*/ default!! Enter radius :
那麼,爲什麼它跳過讀取用戶的選擇並直接跳轉到默認? 有什麼問題?
,你需要初始化'quit'。 –
調試此問題的正確方法是在調試器中逐步執行程序,在每個可疑步驟檢查相關值,或插入語句以在執行過程中打印值以顯示正在進行的操作。要求他人爲你調試是不合適的。 –