2017-07-07 32 views
2

我正在使用此代碼來計算輸入的平方和立方體,並且我添加了第三個選項來計算值除以2,所以我不得不將結果從int修改爲double,但我認爲我沒有做到這一點。這是我到目前爲止:如何在此while循環中將int更改爲double,C

#include <stdio.h> 
// Function prototypes int Square(int value); int Cube(int value); 

int main() 
{ 
    /* variable definition: */ 
    int intValue, menuSelect,Results; 
    double doubleValue, Resultsb; 
    intValue = 1; 
    doubleValue = intValue; 
    // While a positive number 
    while (intValue > 0) {  
     printf ("Enter a positive Integer\n: "); 
     scanf("%d", &intValue); 
     doubleValue=intValue; 
     if (intValue > 0) { 
      printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to Shrink 
        value in half \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) { 
       Resultsb = Shrink(doubleValue); 
       printf("%f shrunk in half is %f\n", doubleValue,Resultsb); 
      } 
      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; 
} 
double Shrink(double value) 
{ return value/2; 
} 

如果有人能告訴我我做錯了什麼,那將是驚人的,謝謝!

+0

您不需要將輸入轉換爲雙精度型。當你調用'Shrink()'時,它會自動轉換,因爲參數是雙精度。 – Barmar

+2

爲什麼你將原型註釋掉了?你還需要一個'Shrink()'的原型。 – Barmar

+0

您需要在'main()'之前''Shrink()''的聲明(aka prototype)。否則,你的程序的行爲是未定義的(C的早期版本,因爲編譯器會在調用點假定'Shrink()'返回'int'並相應地處理它)或不會編譯(最新版本的C)。 – Peter

回答

2

在調用函數之前,您需要函數的原型。否則,默認返回類型是int。所以說:

int Square(int value); 
int Cube(int value); 
double Shrink(double value); 

main()之前,或將函數定義移到頂部。您也不需要doubleValue變量。將函數intValue傳遞給函數會根據原型自動轉換它。

這是工作程序。

#include <stdio.h> 
// Function prototypes 
int Square(int value); 
int Cube(int value); 
double Shrink(double value); 

int main() 
{ 
    /* variable definition: */ 
    int intValue, menuSelect,Results; 
    double Resultsb; 
    intValue = 1; 
    // While a positive number 
    while (intValue > 0) {  
     printf ("Enter a positive Integer\n: "); 
     scanf("%d", &intValue); 
     if (intValue > 0) { 
      printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to Shrink value in half \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) { 
       Resultsb = Shrink(intValue); 
       printf("%d shrunk in half is %f\n", intValue,Resultsb); 
      } 
      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; 
} 
double Shrink(double value) 
{ return value/2; 
} 
+0

謝謝!這工作完美:D。 –

0

發佈時,您的程序無法編譯,因爲您省略了Shrink()的原型。

  • 當編譯行Resultsb = Shrink(doubleValue);,編譯器推斷Shrink需要double參數並返回一個int(是的,它是!)。

  • 當它後來到達Shrink()的定義時,實際定義與先前推斷的原型不兼容。

你應該在main()函數之前移動你的函數,或者至少爲它們提供適當的原型。

在現代C(C99及更高版本)中,您必須定義所有功能或爲其調用之前的。上述行爲適用於舊式C,這仍然受到許多編譯器的支持。