这是我的第一门编程课。当我在选择中选择数字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