2016-03-06 33 views
1

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

嗯。 'Shrink'返回一个'int',对吧? – usr2564301

+0

当你在调试器中完成这个任务时,你学到了什么? –

+0

你是如何将10.4除以2的? '10.4'不是'%d'的有效输入,它不能存储在'int'中。 – MikeCAT

回答

2

首先最新的

floatshrink;

在主要的第二行,你的程序甚至不应该编译。

,你遇到的问题是

1)您不能输入浮到你的程序。你的变量都是整数,你只能扫描整数。

int intValue,menuSelect,results;

//(...)

的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更清晰,但这是一个品味问题。

0

必须使用floatdouble值准确存储浮点数和你Shrink函数必须返回floatdouble,不intprintf格式也需要调整。

此外,您必须在使用main之前定义或至少声明函数。

这里是一个更正的版本。

#include <stdio.h> 

/* 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 */ 
double Shrink(int value) { 
    return (double)value/2; 
} 

int main() { 
    /* variable definition: */ 

    int intValue, menuSelect, results; 
    double 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 
       shrink = Shrink(intValue); 
       printf("The quotient of %d is %g\n", intValue, shrink); 
      } else { 
       printf("Invalid menu item, only 1, 2 or 3 is accepted\n"); 
      } 
     } 
    } 
    return 0; 
} 
相关问题