Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

this code isnt working the way its supposed to, right after taking shape input its not performing the rest

I have been trying to program a tool, where I take input from the user if its a circle or square or rectangle and accordingly it will take the measurement inputs and print out the area for the same. But its not working, please help me with what are the problems with my code and how can i get it to work?

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

float square(float side);
float circle(float rad);
float rect(float a,float b);

int main(){

    char lol;
    
    printf("press s for square\n c for circle\n r for rectagle\n");
    scanf("%c ", &lol);

    if(lol == 's')
    {
        int t;
        printf("enter side measurement");
        scanf("%d", &t);
        printf("the area is %f", square(t));

        
    }
    if(lol == 'c');
    {
        int r;
        printf("enter radius of circle");
        scanf("%d", &r);
        printf("the area is %f", circle(r));


    }
    if (lol == 'r')
    {
        int m,n;
        printf("enter 2 sides ");
        scanf("%d %d", &m,&n);
        printf("the area is %f", rect(m,n));
    }

    return 0;
}

float square(float side){
    return side*side;
}

float circle(float rad){
    return 3.14* rad*rad;
}

float rect(float a,float b){
    return a*b;
}

Screenshot of execution

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want to remove the trialing ‘ ‘ in the first scanf(), and remove the ; after the if statement.

If main() is last you often can get away with not specifying prototypes for small programs like this. Added error checking of scanf(). Prefer if-else-if when the conditions are mutually exclusive, or as here a switch statement so you don’t need to repeat the lol == . Prettied up the prompts a bit (colons, newlines), and sorted both the menu and matching implementation. It makes it easier for end-users and easier to navigate for anyone working on the code. math.h defined the constant M_PI if __USE_XOPEN is set. It’s better to use a constant than hard-coding the 3.14 value in circle().

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

float circle(float rad) {
    return M_PI * rad * rad;
}

float rect(float a, float b) {
    return a * b;
}

float square(float side) {
    return side * side;
}

int main(void) {
    printf("press:\n"
           " c for circle\n"
           " r for rectangle\n"
           " s for square\n"
    );
    char lol;
    if(scanf("%c", &lol) != 1) {
        printf("scanf failed\n");
        return 1;
    }
    switch(lol) {
        case 'c': {
            printf("enter radius of circle: ");
            int r;
            if(scanf("%d", &r) != 1) {
                printf("scanf failed\n");
                return 1;
            }
            printf("the area is %f\n", circle(r));
            break;
        }
        case 'r': {
            printf("enter 2 sides: ");
            int m, n;
            if(scanf("%d %d", &m,&n) != 2) {
                printf("scanf failed\n");
                return 1;
            }
            printf("the area is %f\n", rect(m,n));
            break;
        }
        case 's': {
            printf("enter side measurement: ");
            int t;
            if(scanf("%d", &t) != 1) {
                printf("scanf failed\n");
                return 1;
            }
            printf("the area is %f\n", square(t));
            break;
        }
        default:
            printf("invalid selection %c\n", lol);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading