這是我的第一門編程課。當我在選擇中選擇數字3時出現錯誤。也就是說,如果我將12除以2,程序會給出正確的輸出,但是如果我將10.4除以2,程序輸出就會進入循環,直到停止程序。用十進制除數時得到正確答案的麻煩
#include <stdio.h>
int main() {
/* variable definition: */
int intValue, menuSelect, results;
float shrink;
intValue = 1;
// While a positive number
while (intValue > 0) {
printf("Enter a positive Integer\n: ");
scanf("%d", &intValue);
{
printf("Enter 1 to calculate Square, 2 to Calculate Cube, or 3 to divide input by 2, to end program enter a negative integer.\n: ");
scanf("%d", &menuSelect);
if (menuSelect == 1) {
// Call the Square Function
results = Square(intValue);
printf("Square of %d is %d\n", intValue, results);
} else
if (menuSelect == 2) {
// Call the Cube function
results = Cube(intValue);
printf("Cube of %d is %d\n", intValue, results);
} else
if (menuSelect == 3) {
// Call the Divisor function
results = Shrink (intValue);
printf("The quotient of %d is %d\n", intValue, results);
} else
printf("Invalid menu item, only 1, 2 or 3 is accepted\n");
}
}
return 0;
}
/* function returning the Square of a number */
int Square(int value) {
return value * value;
}
/* function returning the Cube of a number */
int Cube(int value) {
return value * value * value;
}
/* function returning the quotient of a number */
int Shrink(int value) {
return (double)value/2;
}
嗯。 'Shrink'返回一個'int',對吧? – usr2564301
當你在調試器中完成這個任務時,你學到了什麼? –
你是如何將10.4除以2的? '10.4'不是'%d'的有效輸入,它不能存儲在'int'中。 – MikeCAT