Home > OS >  Why there's new entry after the if statement
Why there's new entry after the if statement

Time:01-03

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

int main(){
    float radius;
    float keliling;
    float luas;
    const float PI = 3.14;
    char choice;
    luas = 0;
    keliling = 0;
    
    printf("Luas or Keliling :\n");
    scanf("%s", &choice);

    if(choice = luas){
        printf("Enter radius :\n");
        scanf("%f", &radius);
        keliling = 2 * PI * radius;
        luas = PI * pow(radius, 2);
        printf("Luas lingkaran adalah : %.2f\n", luas);
        
    }
    else if(choice = keliling){
        printf("Enter radius :\n");
        scanf("%f", &radius);
        keliling = 2 * PI * radius;
        luas = PI * pow(radius, 2);
        printf("Keliling lingkaran adalah : %.2f\n", keliling);
    }

    return 0;


}

when i run the code, the code goes to if statement for choose beetwen luas and keliling and i choose luas but after i choose luas, there's a new entry, and whatever i fill the entry with any word it goes"blablabla is not recognized as an internal or external command, operable program or batch file."

try to find the error and fix the code

CodePudding user response:

Assignment vs Comparison:

if(choice = luas)

This initializes choice — which is of type char — with luas, which is of type float.

= is for assignment, == is for comparison. That being said, you can't compare strings with ==, use standard strcmp. == will only compare the pointer values.

Incorrect format specifier:

scanf("%s", &choice);

This invokes undefined behaviour. choice is declared to be of type char. The correct format specifier for a char is %c. But then you want the user to enter a string, so scanf will read one char and leave the trailing junk in the input buffer, which might automatically be read by subsequent calls to input functions, and you'll never be prompted for input.

CodePudding user response:

The problem in your code is that luas and keiling are floats and you are trying compare them with a string. Just like @Haris mentioned.

and = is assignment. you should use == to compare. Regardless you can also use switch(choice) if you want.

you should also take the radius scanning out of your if condition. also in following code choice of luas is 0 and keliling is 1. In enum if values are not explicitly mentioned it start from 0

A working version of your code will look like this:

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

#define PI 3.14

enum Choice {
    luas, /* Area of a circle  */   /* luas = 0 */
    keliling /* circumference of a circle */ /* keliling = 1 */
};

int main ( void ) {
    float radius;
    double result;
    int choice;

    printf("Luas or Keliling :\n");
    scanf("%d", &choice);
    printf("Enter radius :\n");
    scanf("%f", &radius);

    switch(choice) {
        case luas:
        result = PI * pow(radius, 2);
        printf("Luas lingkaran adalah : %.2f\n", result);
        break;

        case keliling:
        result = 2 * PI * radius;
        printf("Keliling lingkaran adalah : %.2f\n", result);
        break;
    }

    return 0;
} 


enter image description here

  • Related