2

Hi guys I'm a beginner in C and I'm trying to make a simple program where you calculate the perimeter and/or area in a triangle. Whenever I run this program and after the output (area or perimeter) is shown, the first set of text (where you input A, P or S) repeats itself twice rather than just once (image is found in the link): https://www.dropbox.com/s/5nz5az9tkgjv24e/Capture2.JPG?dl=0

Is there something wrong with my code? I can't seem to find the error

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

float triangleArea (float base, float height) 
{
float area; 
area = (base * height) / 2;
return (area); 
}

float trianglePerimeter (float side1, float side2, float side3) 
{
float perimeter;
perimeter = side1 + side2 + side3; 
return (perimeter); 
}

int main(int argc, char *argv[])
{
  char select; 
  float input1, input2, input3, output; 

   do {

    printf("Type A for Area \n"); 
    printf("Type P for Perimeter \n"); 
    printf("Type S to stop \n"); 
    printf("Input: "); 
    scanf("%c", &select); 

    switch (select) {
        case 'A' : 
            printf("Input base: "); 
            scanf("%f", &input1); 
            printf("Input height: "); 
            scanf("%f", &input2); 
            output = triangleArea(input1, input2); 
            printf("Area: %.2f \n", output); 
            break; 
        case 'P' : 
            printf("Input all 3 sides: \n"); 
            scanf("%f%f%f", &input1, &input2, &input3); 
            output = trianglePerimeter(input1, input2, input3); 
            printf("Perimeter: %.2f \n", output); 
            break;
        case 'S' : 
            printf("END \n"); 
            break; 
    }
    printf("\n"); 
} while (select != 'S'); 



  getchar();    
  return 0;

 }
knight3894
  • 21
  • 2

0 Answers0