//   
#include <iostream>
using namespace std;
#include <cmath>    
#include <math.h>

int main()
{
    float a, b, c, t, s, area; 
    cout << "Enter the lengths of your triangle's sides: ";
    cin >> a >> b >> c;
    if(a>b) {t=a; a=b; b=t;};
    if(b>c) {t=b; b=c; c=t;};
    if(a>b) {t=a; a=b; b=t;};
    
if((c*c) < (a*a)+(b*b))
        cout << "Your triangle is an acute ";
    else
    if((c*c) == (a*a)+(b*b))
        cout << "Your triangle is a right ";
    else
    if((c*c) > (a*a)+(b*b))
        cout << "Your triangle is an obtuse ";


    if(a==b&&b==c)
        cout << "equilateral triangle.";
    else
    if(a!=b&&b!=c&&c!=a)
        cout << "scalene triangle.";
    else
    if(a==b&&b!=c||b==c&&b!=a)
        cout << "isosceles triangle." << endl;
    
    s = ((a+b+c) / 2);
    area = sqrt(s * (s-a) * (s-b) * (s-c));
    cout << " The area of the triangle is " << area << endl;
    system ("pause");
}

How would I make this program output an error message and exit the program if the user inputs a letter instead of a number or if the user inputs three lengths that do not create a triangle?

I have tried this for the inputs that do not create a triangle

if(a+b<c)
   cout << "Your lengths do not create a triangle" << endl;
   exit(EXIT_FAILURE);

but it closes the program even if the inputs were valid.

nvm. I worked it out.

if(!(cin >> a >> b >> c)){
        cout << "You must only enter numbers." << endl;
        system("pause");
        exit(EXIT_FAILURE);
}
    if(a+b<c){
        cout << "The inputs do not create a triangle." << endl;
        system("pause");
        exit(EXIT_FAILURE);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.