2016-03-06 40 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; 
} 
相關問題