Question

My concern is that the program is only running once... In this assignment we will develop an application to calculate the area and perimeter of geometric shapes. First the user is asked to enter a letter representing the shape. We use C for circle, R for rectangle and S for square. After the user chooses the shape, the program prompts for the appropriate dimensions of the shape accordingly. For instance, if the user has chosen a square, the program will ask for a side. If it’s a circle, the program will ask for radius. If it’s a rectangle, it will ask for length and width. Upon receiving the appropriate dimensions, the program will calculate the area and the perimeter of the requested shape and print it on the screen. And again, the code will ask for another letter. If the user enters ‘Q’ the program terminates.

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

int main()
{
    float PI = 3.1415;
    char choice;
    float area, parameter;
    int radius, side, length, width;

    do{
        printf("Please enter a shape(C:Circle, S:Square, R:Rectangle, Q:Quiit> ");
        scanf("%s", &choice);

        switch(choice){
        case 'C':
            printf("Enter a radius of the circle: ");
            scanf("%d", &radius);
            area = (2*radius)*PI;
            parameter = 2*PI*radius;
            printf("The area of the circle is %.02f and parameter is %.02f", area, parameter);
        break;

        case 'S':
            printf("Enter the side of the square: ");
            scanf("%d", &side);
            area = side * side ;
            parameter = 4*side;
            printf("The area of the circle is %.02f and parameter is %.02f", area, parameter);
        break;

        case 'R':
            printf("Enter the width of the rectangle: ");
            scanf("%d", &width);
            printf("Enter the length of the rectangle: ");
            scanf("%d", &length);
            area = length*width;
            parameter = (2*length)+(2*width);
            printf("The area of the circle is %.02f and parameter is %.02f", area, parameter);
        break;

        case 'Q':
            printf("Thank and bye");
        break;

        default:
            printf("Invalid input");

    }
        return 0;
    } while (choice != 'Q');
}

No correct solution

OTHER TIPS

It is only running once because you are using a return statement inside the while loop:

return 0;

A return statement in main will end your program when hit. Since it is inside the while loop, it will never loop. The first time it is hit it will end the program, and you'll never get to loop.

Move that below the while loop:

} while (choice != 'Q');
return 0;

You need to move the return 0 below the loop.

You have return 0 inside the while loop. So as soon as the switch statement ends, programs returns 0 and is not able to check the while loop condition. Move the return 0 out of the loop.

} while (choice != 'Q'); return 0;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top