-1

i try to ask the user after a process of calculation that if he wants to do more calculation for that i use do while loop but for some reason it should first calculate area then it should ask user if he wants to calculate more the it should repeat or exit

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    int option;
    float Area, Radius, Length, Width, Height, Circumference, Volume;
    char select;
    do {
        printf("\nPlease select the option\n");
        printf(
            "1.Area and Circumference of circle \n2.Area of rectangle \n3.Area of "
            "triangle \n4.Volume of sphere");
        printf("\nEnter your selection : ");
        scanf("%i", &option);
        system("cls");
        switch(option) {
        case 1:
            printf("Enter the radius of the circle : ");
            scanf("%f", &Radius);
            Area = 3.14159 * pow(Radius, 2);
            Circumference = 2 * 3.14159 * Radius;
            system("cls");
            printf("Area of circle is %f Circumference of circle is %f", Area,
                   Circumference);
            break;
        case 2:
            printf("Enter length  of the rectangle :");
            scanf("%f", &Length);
            system("cls");
            printf("Enter  width of the rectangle :");
            scanf("%f", &Width);
            system("cls");
            Area = Length * Width;
            printf("The Area of rectangle is %f", Area);
            break;
        case 3:
            printf("Enter the height of triangle :");
            scanf("%f", &Height);
            system("cls");
            printf("Enter the lenght of triangle :");
            scanf("%f", &Length);
            system("cls");
            Area = Height * Length / 2;
            printf("The area of the triangle is %f", Area);
            break;

        case 4:
            printf("Enter the radius of the sphere :");
            scanf("%f", &Radius);
            system("cls");
            Volume = 4 * 3.14159 * pow(Radius, 3) / 3;
            printf("The surface area of the sphere is %f", Volume);
            break;
        default:
            printf("Invalid option\t\t");

            break;
        }
        printf("\nDo you want to do another calculation");
        scanf("%c", &select);
    } while(select == 'y');
}

does not work

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Arie3s
  • 1
  • 1
  • Why is this tagged css ? – Cid Sep 01 '20 at 07:41
  • 1
    Please format your code properly, it’s completely unreadable at present. – Konrad Rudolph Sep 01 '20 at 07:43
  • 2
    See [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). The character you read with `scanf("%c",&select);` will be that newline. To read the first non-whitespace character use `" %c"` instead. – dxiv Sep 01 '20 at 07:46
  • This may compile as a C++ program, but it looks like pure C. – Ted Lyngmo Sep 01 '20 at 07:51
  • After you have read a number, the next thing in the input stream is a whitespace character. And reading a single character does not skip over whitespace. – molbdnilo Sep 01 '20 at 08:11
  • thank you for your guidance will implement,than you for your help – Arie3s Sep 01 '20 at 12:22

1 Answers1

0

The code is running perfect, you just have to flush the input buffer before taking next input, You can use fflush(stdin). I added that line in your code.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
    int option;
    float Area, Radius, Length, Width, Height, Circumference, Volume;
    char select;
    do {
        printf("\nPlease select the option\n");
        printf(
            "1.Area and Circumference of circle \n2.Area of rectangle \n3.Area of "
            "triangle \n4.Volume of sphere");
        printf("\nEnter your selection : ");
        scanf("%i", &option);
        system("cls");
        switch(option) {
        case 1:
            printf("Enter the radius of the circle : ");
            scanf("%f", &Radius);
            Area = 3.14159 * pow(Radius, 2);
            Circumference = 2 * 3.14159 * Radius;
            system("cls");
            printf("Area of circle is %f Circumference of circle is %f", Area,
                   Circumference);
            break;
        case 2:
            printf("Enter length  of the rectangle :");
            scanf("%f", &Length);
            system("cls");
            printf("Enter  width of the rectangle :");
            scanf("%f", &Width);
            system("cls");
            Area = Length * Width;
            printf("The Area of rectangle is %f", Area);
            break;
        case 3:
            printf("Enter the height of triangle :");
            scanf("%f", &Height);
            system("cls");
            printf("Enter the lenght of triangle :");
            scanf("%f", &Length);
            system("cls");
            Area = Height * Length / 2;
            printf("The area of the triangle is %f", Area);
            break;

        case 4:
            printf("Enter the radius of the sphere :");
            scanf("%f", &Radius);
            system("cls");
            Volume = 4 * 3.14159 * pow(Radius, 3) / 3;
            printf("The surface area of the sphere is %f", Volume);
            break;
        default:
            printf("Invalid option\t\t");

            break;
        }
        printf("\nDo you want to do another calculation : ");
        fflush(stdin);  // Flushing the input buffer
        scanf("%c", &select);
    } while(select == 'y');
}

EDIT - As pointed out by @David C. Rankin and @molbdnilo about the undefined behavior in comments and through this thread, I will not encourage you for using fflush as regular practice, you can even simply write your scanf("%c", &select); as scanf(" %c", &select); or you can create another temporary character variable to soak the new line input character like.

scanf("%c", &select);

replaced with -

char temp;
scanf("%c", &temp);
scanf("%c", &select);.

dukeforever
  • 298
  • 1
  • 7