vendredi 7 juillet 2017

How to change an int to a double in this while loop, C

I'm working with this code to calculate the square and cube of an input, and I added a third option to calculate the value divided by 2, so I had to change the result from an int to a double, but I don't think I did it right. Here's what I have so far:

 #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;
}

If anyone can tell me what I'm doing wrong that would be amazing, thank you!

Aucun commentaire:

Enregistrer un commentaire