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

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    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>");
                scanf_s("%c", &answer);
            }
            else if ( a == b ) && ( b == c )
            {
                printf("Its an Equilateral Triangle\n");
                printf("Do you wish to continue with a new triangle?<y/n>");
                scanf_s("%c", &answer);
            }
            else
            {
                printf("Its an Scalene Triangle\n");
                printf("Do you wish to continue with a new triangle?<y/n>");
                scanf_s("%c", &answer);
            }
        }
       
        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>");
            scanf("%c, &answer");
        }
        }
        while (answer == y || answer == Y);
       
        }
        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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    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>");
    		scanf_s("%c, &answer");
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Adak View Post
    I've never quite wrapped my ahead around the need for bools[...]
    They're unambiguous. And that's about it.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    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. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CommonTater View Post
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    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++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks for that, laserlight. The "double-bang" trick was slick.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    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. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    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 
    			
    			printf ("Please enter either (Y/N)\n");
    		
    	}
    	while(ok == 1);
    
    printf( "Program has ended...\n" );
    
    return 0;
    
    }

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Not sure of what you are expecting to get there, as you never allocated any space for buf. As for clearing the input buffer, have a look at this and this.

Popular pages Recent additions subscribe to a feed