此處的所有內容都以我希望的方式工作,但函數「tipConvert(tip)」除外。提示計算 - 將數字更改爲小數點代碼
我希望它將數字(如15)更改爲.15,該數字可能是百分比的十進制表示形式。
#include <stdio.h>
float tipConvert(int tip);
int main()
{
char value;
int intValue tip;
float bill, result;
intValue = 1;
while(intValue == 1)
{
printf("Would you like to calculate a tip: y or n?\n");
scanf("%s", &value);
if(value == 'y')
{
printf("Enter the bill amount:\n");
scanf("%f", &bill);
printf("Enter the tip amount as an integer:\n");
scanf("%i", &tip);
result = tipConvert(tip)*bill;
printf("Tip for %.2f is %.2f\n", bill,result);
}
else if(value == 'n')
{
printf("Thank you for using Tip Calculator.");
break;
}
else
printf("Please select y or n.\n");
}
return 0;
}
float tipConvert(int tip)
{
return tip/100;
}
請嘗試使您的問題標題不太通用。什麼是「計算」? –
你是dividint 2個整數;並將結果存儲爲int。因此,當您的分區結果爲0.xx時,它將舍入爲零。因此,提示/ 100.0:這應該工作。 –