Thread: input validation- finding the area of a triangle and its type(isosceles, ect)

1. input validation- finding the area of a triangle and its type(isosceles, ect)

I believe I am having some problems with my compiler. I believe everything is written correctly, and I was wondering if someone can give me heads up. Maybe I need a new compiler? or I saved the file incorrectly? or it's my operating system/computer.

-----------------------------------------------------------------------------------
Code:
```#include<stdio.h>
#include<math.h>

int main(void)
{
do
{
float a, b, c, s, area;
char answer, y, Y, n, N;

printf("Enter the first side of the triangle: ");
scanf_s("%f", &a);
printf("Enter the second side of the triangle: ");
scanf_s("%f", &b);
printf("Enter the third side of the triangle: ");
scanf_s("%f", &c);
if ( a + b > c) || ( b + c > a) || ( a + c > b)
{
s = (a + b + c) / 2;
area = sqrt(s * (s-a) * (s-b) * (s-c));
printf("Area = %f\n", area);

if ( a== b ) || ( b == c ) || ( a == c )
{
printf("Its an Isosceles Triangle\n");
printf("Do you wish to continue with a new triangle?<y/n>");
}
else if ( a == b ) && ( b == c )
{
printf("Its an Equilateral Triangle\n");
printf("Do you wish to continue with a new triangle?<y/n>");
}
else
{
printf("Its an Scalene Triangle\n");
printf("Do you wish to continue with a new triangle?<y/n>");
}
}

while ( answer != y ) || ( answer != Y ) || ( answer != n ) || ( answer != N )
{
printf("You have made an invalid selection...\n");
printf("Do you wish to continue with a new triangle?<y/n>");
}
}

}
return 0;
}```
COMPILER ERROR-----------------------------------------

------ Build started: Project: triangles lab1b, Configuration: Debug Win32 ------
triangles.cpp
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(23): error C2143: syntax error : missing ';' before '||'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(23): warning C4390: ';' : empty controlled statement found; is this the intent?
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(24): error C2143: syntax error : missing ';' before '{'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(28): error C2143: syntax error : missing ';' before '||'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(28): warning C4390: ';' : empty controlled statement found; is this the intent?
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(29): error C2143: syntax error : missing ';' before '{'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(34): error C2181: illegal else without matching if
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(34): error C2143: syntax error : missing ';' before '&&'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(34): warning C4390: ';' : empty controlled statement found; is this the intent?
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(35): error C2143: syntax error : missing ';' before '{'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(40): error C2181: illegal else without matching if
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(47): error C2143: syntax error : missing ')' before '!'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(47): error C2065: 'y' : undeclared identifier
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(47): error C2059: syntax error : ')'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(47): error C2143: syntax error : missing ')' before '!'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(47): error C2059: syntax error : ')'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(48): error C2143: syntax error : missing ';' before '{'
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(53): error C2065: 'y' : undeclared identifier
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(53): error C2065: 'Y' : undeclared identifier
c:\temp\triangles lab1b\triangles lab1b\triangles.cpp(53): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2. Code:
```
if ( a + b > c) || ( b + c > a) || ( a + c > b)```
Above if statement (and other if statements in your program have the same problem), is incorrect. Correct would be:

Code:
`if(( a + b > c) || ( b + c > a) || ( a + c > b))`
An if statement ends immediately after the first closing parentheses, which matches the number of opening parentheses. When one ( is matched by one ), *it's toast*.

if(a+b>c)<=== the if statement has ended, since you matched the number of opening parenthesis.

3. woot

thanks so much that was the problem can't believe I overlooked that lol. Now I gotta figure a way around that
Code:
```while (( answer != 'y' ) || ( answer != 'Y' ) || ( answer != 'n' ) || ( answer != 'N' ))
{
printf("You have made an invalid selection...\n");
printf("Do you wish to continue with a new triangle?<y/n>");
prob convert to a nested if statement that has a new variable to keep track if its true. wish C had bools would make things easier for me

4. Code:
`    while (answer == y || answer == Y);`
You're also going to run into problems with this... y and Y are uninitialized numeric variables, not the letters "y" and "y". The results of the conditional statement will be undefined and unpredictable.

5. I've never quite wrapped my ahead around the need for bool's - if you can count from zero to one, and back again, what does bool give you extra -- besides a "whopee! I've got a bool", feeling.

I'm not Catholic, but I decided to give them up in C++ for Lent last year, anyway.

I've never quite wrapped my ahead around the need for bools[...]
They're unambiguous. And that's about it.

I've never quite wrapped my ahead around the need for bool's - if you can count from zero to one, and back again, what does bool give you extra -- besides a "whopee! I've got a bool", feeling.

I'm not Catholic, but I decided to give them up in C++ for Lent last year, anyway.
To give you one good example... The result of every "if" statement operates upon a boolean true or false produced by the expression in brackets behind it...

if (6 > 3) == true, do it

if (6 < 3) == false, don't do it

Booleans are useful as very simple "go" "no go" flags that help organize program execution.

8. Originally Posted by CommonTater
To give you one good example... The result of every "if" statement operates upon a boolean true or false produced by the expression in brackets behind it...

if (6 > 3) == true, do it

if (6 < 3) == false, don't do it

Booleans are useful as very simple "go" "no go" flags that help organize program execution.
OK, but EVERY expression being evaluated in C, reduces to either true (non-zero), or false (zero).

So there is your "go, or no go", right there. Either the variable equals zero or it doesn't. Seems clear to me.

Either the variable equals zero or it doesn't. Seems clear to me.
What is not always clear is whether the integer value is a boolean value or an integral value. The use of a boolean type clarifies this.

I suggest that you read up on the safe bool idiom: what motivated its invention and use, and its replacement by the explicit conversion function feature that will be introduced in the next version of C++.

10. Thanks for that, laserlight. The "double-bang" trick was slick.

11. i think the logic of your program isn't quite correct... for eg..

Code:
`if(( a + b > c) || ( b + c > a) || ( a + c > b))`
should be..

Code:
`if(( a + b > c) && ( b + c > a) && ( a + c > b))`
as the condition for a triangle is that the sum of any two sides is greater than the third.. if you use ||, this condition may not be checked against.

similarly, your program will not be able to detect equilateral triangles and categorize them as isosceles...

12. Not sure what is going on here but I am not getting the right results

I am trying to get his to work, so far my output has been getting messed up, I am not sure if there is a way to clear the input buffer in C been trying to use scanf( "%*", &buf);.

Code:
```#include <stdio.h>
#include <math.h>

int main(void)

{
int side1, side2, side3;
int ok;
float s, area;
char y, n, Y, N, user_input, buf;
Y=1;
y=1;
N=0;
n=0;
ok=0;

do
{

printf( "Enter the First side of the Triangle:\n" );

scanf( "%d", &side1);

printf( "Enter the Second side of the Triangle:\n" );

scanf(  "%*", &buf);
scanf( "%d", &side2);

printf( "Enter the Third side of the Triangle:\n" );

scanf(  "%*", &buf);
scanf(  "%*", &buf);
scanf( "%d", &side3);

printf("%d\n, %d\n, %d\n", side1, side2, side3);

if ((side1 + side2 <= side3) || (side1 + side3 <= side2) || (side2 + side3 <= side1))

{

printf( "Not a Valid Triangle\n" );

}

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

{

printf( "Triangle is Equilateral\n" );

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

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

printf( "Area of the Triangle is:\n%d\n", area);

}

else if ((side1 != side2) && (side1 != side3) && (side2 !=side3))
{

printf( "Triangle is Scalene\n" );

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

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

printf( "Area of the Triangle is:\n%d\n", area);

}

else if ((side1 == side2) || (side1 == side3) || (side2 == side3))

{

printf( "Triangle is Isosceles\n" );

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

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

printf( "Area of the Triangle is:\n%d\n", area);

}

printf( "Would you like to create another triangle? (Y/N)\n" );

scanf(  "%*", &buf);
scanf(  "%c", &user_input);

if ((user_input == 'y') || (user_input == 'Y'))

{

ok=1;

}

else if ((user_input == 'n') || (user_input == 'N'))

{

ok=0;

}

else