这是我的第一门编码课程。在选择中选择数字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;
}
首先是什么
浮动收缩
在主代码的第二行,您的程序甚至不应编译。
您遇到的问题是
1)您不能在程序中输入浮点数。您的变量都是整数,并且只扫描整数。
int intValue,menuSelect,结果;
//(...)
scanf(“%d”,&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相比,该代码看起来更加清晰,但这只是一个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句