Why is my C++ program generating nonsense math?

Try the below code. The perimeter and s computation need to happen after you initialize values for the sides of your triangle.

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b; 
    int c;

    cout << "Enter three numbers for the lengths of a triangle with sides a, b, and c. The first number should be the smallest, and the last should be the longest.";
    cin >> a;
    cin >> b;
    cin >> c;

    cout << "Lengths " ;
    cout << a; 
    cout << " " ;
    cout << b; 
    cout << " ";
    cout << c;

    double perimeter = (a + b + c);
    int s=0.5*(a + b + c);

    cout << "The perimeter of the triangle is ";
    cout << perimeter ;

    cout << "s equals " ;
    cout << s;
}

Leave a Comment