-8

I've been trying to solve this error for half an hour now. How do I get this code to work?

Here's the input, please point out my mistake(s).

#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();
}
vaultah
  • 44,105
  • 12
  • 114
  • 143

4 Answers4

2

Use braces if a if/else block has multiple lines:

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...");

Without braces your code is interpreted like

// 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) : "); 
vaultah
  • 44,105
  • 12
  • 114
  • 143
1
  1. Use if..else if..else block properly with braces.

  2. If you are using GCC compiler then use following code and compile code with math library.

For example (correct way): gcc test.c -lm

otherwise it gives error : undefined reference to 'pow' (math library function)

#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;
}
Community
  • 1
  • 1
RDX
  • 409
  • 6
  • 22
0

You are missing the curly braces {}. Do: if() { ... } else if() { ...}. You should take a look at: if statement without brackets

Community
  • 1
  • 1
Sergio
  • 143
  • 12
0

missing brackets in if else command

#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();
}
boyshot17
  • 39
  • 3