2015-09-13 53 views
-8

我一直試圖解決這個錯誤半個小時了。 如何獲得此代碼的工作?如何解決錯誤:其他沒有以前如果

以下是輸入,請指出我的錯誤。

#include <stdio.h> 
#include <math.h> 

void main() 
{ 
const float pi = 3.142; 
int choice; 
float radius, volume, volts, ohms, watts, mass, accel, force; 

printf("\n1. Calculate the volume of a sphere"); 
printf("\n2. Calculate the power of a circuit"); 
printf("\n3. Calculate the force of an object"); 
printf("\n-----------------------------------\n"); 
printf("What is your choice? : "); 
scanf("%d", &choice); 

if(choice==1) 
    printf("Enter value of radius (cm) : "); 
    scanf("%f", &radius); 
    volume = 4/3 * pi * pow(radius,3); 
    printf("Volume of a sphere is %.2f", volume); 
    else if(choice==2) 
     printf("Enter value of voltage (volts) : "); 
     scanf("%f", &volts); 
     printf("Enter value of resistance (ohms) : "); 
     scanf("%f", &ohms); 
     watts = pow(volts,2)/ohms; 
     printf("Power of circuit is : %.2f watts", watts); 
     else if(choice==3) 
      printf("Enter mass of object (kg)    : "); 
      scanf("%f", &mass); 
      printf("Enter acceleration (meters per second squared) : "); 
      scanf("%f", &accel); 
      force = mass * accel; 
      printf("The force of the object is : %.2f Neutons", force); 
      else 
       printf("You've entered an invalid choice..."); 
getch(); 
} 
+0

C++ **或* * C?看起來更像C,壽。 – Downvoter

+3

在您的代碼中正確放置大括號('{}'),以解決錯誤。 –

+0

@cad:看起來更像Python(但仍然格式不正確)。也許OP認爲每種編程語言都使用縮進作爲語法元素? – Olaf

回答

2

使用大括號如果if/else塊有多條線路:

if (choice==1) { 
    printf("Enter value of radius (cm) : "); 
    scanf("%f", &radius); 
    volume = 4/3 * pi * pow(radius,3); 
    printf("Volume of a sphere is %.2f", volume); 
} else if (choice==2) { 
    printf("Enter value of voltage (volts) : "); 
    scanf("%f", &volts); 
    printf("Enter value of resistance (ohms) : "); 
    scanf("%f", &ohms); 
    watts = pow(volts,2)/ohms; 
    printf("Power of circuit is : %.2f watts", watts); 
} else if (choice==3) { 
    printf("Enter mass of object (kg)    : "); 
    scanf("%f", &mass); 
    printf("Enter acceleration (meters per second squared) : "); 
    scanf("%f", &accel); 
    force = mass * accel; 
    printf("The force of the object is : %.2f Neutons", force); 
} else printf("You've entered an invalid choice..."); 

沒有括號代碼這樣解釋

// Standalone if block 
if (choice==1) 
    printf("Enter value of radius (cm) : "); 
// End of the if block 
// ... 
// else without previous if (syntax error) 
else if (choice==2) 
    printf("Enter value of voltage (volts) : "); 
0

缺少括號內的if else命令

#include <stdio.h> 
void main() 
{ 
const float pi = 3.142; 
int choice; 
float radius, volume, volts, ohms, watts, mass, accel, force; 

printf("\n1. Calculate the volume of a sphere"); 
printf("\n2. Calculate the power of a circuit"); 
printf("\n3. Calculate the force of an object"); 
printf("\n-----------------------------------\n"); 
printf("What is your choice? : "); 
scanf("%d", &choice); 

if(choice==1) 
{ 

    printf("Enter value of radius (cm) : "); 
    scanf("%f", &radius); 
    volume = 4/3 * pi * pow(radius,3); 
    printf("Volume of a sphere is %.2f", volume); 
} 
else if(choice==2) 
{ 
     printf("Enter value of voltage (volts) : "); 
     scanf("%f", &volts); 
     printf("Enter value of resistance (ohms) : "); 
     scanf("%f", &ohms); 
     watts = pow(volts,2)/ohms; 
     printf("Power of circuit is : %.2f watts", watts); 
} 
else if(choice==3) 
{ 

      printf("Enter mass of object (kg)    : "); 
      scanf("%f", &mass); 
      printf("Enter acceleration (meters per second squared) : "); 
      scanf("%f", &accel); 
      force = mass * accel; 
      printf("The force of the object is : %.2f Neutons", force); 
} 
else 
       printf("You've entered an invalid choice..."); 
getch(); 
} 
+1

'if'不是_command_。 –

1
  1. 使用的if..else的if..else用大括號正確阻塞。

  2. 如果您使用的是GCC編譯器,請使用以下代碼並使用數學庫編譯代碼。

例如(正確的方法):GCC test.c的-lm

否則給出錯誤:undefined reference to 'pow'(數學庫功能)

#include <stdio.h> //Standard IO functions 
#include <math.h> //math functions 

int main() 
{ 
const float pi = 3.142; 
int choice; 
float radius, volume, volts, ohms, watts, mass, accel, force; 

printf("\n1. Calculate the volume of a sphere"); 
printf("\n2. Calculate the power of a circuit"); 
printf("\n3. Calculate the force of an object"); 
printf("\n-----------------------------------\n"); 
printf("What is your choice? : "); 
scanf("%d", &choice); 

if(choice==1) 
{ 

    printf("Enter value of radius (cm) : "); 
    scanf("%f", &radius); 
    volume = 4/3 * pi * pow(radius,3); 
    printf("Volume of a sphere is %.2f", volume); 
} 
else if(choice==2) 
{ 
     printf("Enter value of voltage (volts) : "); 
     scanf("%f", &volts); 
     printf("Enter value of resistance (ohms) : "); 
     scanf("%f", &ohms); 
     watts = pow(volts,2)/ohms; 
     printf("Power of circuit is : %.2f watts", watts); 
} 
else if(choice==3) 
{ 

      printf("Enter mass of object (kg)    : "); 
      scanf("%f", &mass); 
      printf("Enter acceleration (meters per second squared) : "); 
      scanf("%f", &accel); 
      force = mass * accel; 
      printf("The force of the object is : %.2f Neutons", force); 
} 
else 
       printf("You've entered an invalid choice..."); 
return 0; 
}