这是我编写的第一门课程。当我在选择中选择数字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;
}
答案 0 :(得分:2)
首先是什么
floatshrink;
在主要的第二行,你的程序甚至不应该编译。
您遇到的问题是
1)您无法在程序中输入浮点数。你的变量都是整数,你只是在扫描整数。
int intValue,menuSelect,results;
//(...)
scanf(“%d”,&amp; intValue);
2)你的函数只返回整数,所以你还是会丢失小数部分。
int Shrink(int value)
{
return (double)value/2;
}
3)不要忘记你的标题!!编译器需要知道会发生什么
double Square(double value);
/* function returning the Cube of a numnber */
double Cube(double value);
/* function returning the quotient of a number */
double Shrink(double value);
你们最后有一些基本的缩进问题,不用担心它会很快得到它 这是更正后的版本
#include <stdio.h>
#include <stdlib.h>
double Square(double value);
/* function returning the Cube of a numnber */
double Cube(double value);
/* function returning the quotient of a number */
double Shrink(double value);
int main (void )
{
/* variable definition: */
float numer_input,results;
int menuSelect;
//floatshrink; (no idea what this is)
numer_input = 1; //you could use a do while statement to avoid this
// While a positive number
while ( numer_input> 0)
{
printf ("Enter a positive Integer\n: ");
scanf("%f", &numer_input);
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(numer_input);
printf("Square of %f is %f\n",numer_input, results);
}
else if (menuSelect == 2)
{
// Call the Cube function
results = Cube(numer_input);
printf("Cube of %f is %f\n",numer_input, results);
}
else if (menuSelect == 3)
{
// Call the Divisor function
results = Shrink (numer_input);
printf("The quotient of %f is %f\n", numer_input, results);
}
else
printf("Invalid menu item, only 1, 2 or 3 is accepted\n");
}
return 0;
}
/* function returning the Square of a number */
double Square(double value){
return value*value;
}
/* function returning the Cube of a numnber */
double Cube(double value){
return value*value*value;
}
/* function returning the quotient of a number */
double Shrink(double value){
return (double)value/2;
}
最后我建议使用switch语句,因为代码看起来比使用if和else if更清晰,但这是一个品味问题。
答案 1 :(得分:0)
您必须使用float
或double
值准确存储浮点数,而Shrink
函数必须返回float
或double
,而不是{{1 }}。 int
格式也需要调整。
此外,您必须在printf
中使用函数之前定义或至少声明这些函数。
以下是更正后的版本。
main