C Programming: Problem with my do-while loop (infinite loops/seg faults)

This is a pretty basic program that inputs the sides of a triangle, and determines what kind it is, and what the area is. I've been having some issues with my do-While - I have a prompt at the end of my code, just before the "while," that asks the user if they want to continue...I've been getting infinite loops.

Right now, it's giving me seg faults. Would definitely appreciate the help.

Thanks!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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

int main(void){

    int side1;
    int side2;
    int side3;

    int userInput = 0;

    float s, area;

    printf("This program will input three sides of a triangle, and then will determine whether it is an Equilateral, Isosceles , Scalene, or neither");

    do{

        printf("\n\nPlease enter the three sides of your triangle, separated by spaces: ");

        scanf("%d %d %d", &side1, &side2, &side3);

        fflush(stdin);


        if (side1 < 0 || side2 < 0 || side3 < 0)

        {
            printf("Error: Please enter positive integers.");
        }

        if (side1 == side2 && side2== side3)

        {
            printf("\nYou have an Equilateral triangle.");
        }

        else if (side1 == side2 || side2 == side3  || side1 == side3 )
        {
            printf("\nYou have an Isosceles triangle.");
        }

        else
        {
            printf("\nYou have a Scalene triangle");
        }

        s = (side1 + side2 + side3) / 2.0;

        area = sqrt((s * (s - side1) * (s - side2) * (s - side3)));

        printf("\n\nThe area of the triange is %f.", area);

        printf("\n\nIf you would like to continue, enter any number. If you wish to quit, enter 99.");
        scanf("%d", userInput);

        fflush(stdin);

    }

    while (userInput != 99);

    return 0;
}
Last edited on
Seems like I forgot to put &userInput on line 52.

Last question is that my professor insisted on flushing the buffer after input is taken. I tried the program both with and without "fflush(stdin);" (on lines 23 and 57) after taking input, and the result is the same. Not sure if I'm doing something wrong there.
Last edited on
Topic archived. No new replies allowed.