Thread: Right Triangle Program

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Wink Right Triangle Program

    Hello to all. Hope everyone is having a good night. I am turning to the message board because I don't know what any of these error messages mean. Hopefully someone out there can help. This program is supposed to calculate the area of a triangle and tell if it is a right triangle or not. Any help is always appreciated.
    John

    Can anyone help me out or point me in the right direction.

    Calculate area of a triangle and tell if its a right triangle or not using a switch
    statement. I am getting error messages on lines
    righttriangle.c:45: error: expected expression before âelseâ
    righttriangle.c:57: error: expected expression before âelseâ
    righttriangle.c:126:9: warning: multi-character character constant
    righttriangle.c: In function âcalAreaâ:
    righttriangle.c:175: error: âfâ undeclared (first use in this function)
    righttriangle.c:175: error: (Each undeclared identifier is reported only once
    righttriangle.c:175: error: for each function it appears in.)
    righttriangle.c:175: error: expected â;â before â}â token
    righttriangle.c: In function âmainâ:
    righttriangle.c:175: error: expected declaration or statement at end of input
    righttriangle.c:175: error: expected declaration or statement at end of input
    righttriangle.c:24: warning: unused variable âhypotenuseâ
    righttriangle.c: In function âcalLeg1â:
    righttriangle.c:161: warning: control reaches end of non-void function
    righttriangle.c: In function âcalLeg2â:
    righttriangle.c:166: warning: control reaches end of non-void function
    righttriangle.c: In function âcalHypotenuseâ:
    righttriangle.c:171: warning: control reaches end of non-void function
    righttriangle.c: In function âcalAreaâ:
    righttriangle.c:175: warning: control reaches end of non-void function
    righttriangle.c: In function âmainâ:
    righttriangle.c:175: warning: control reaches end of

    Code:
     10 #include <stdio.h>
     11 #include <math.h>
     12 
     13 /* Function Prototypes */
     14 void printMenu();
     15 void float calLeg1(float leg2, float hypotenuse);
     16 
     17 void float calLeg2(float leg1, float hypotenuse);
     18 void float calHypotenuse(float leg1, float leg2);
     19 void float calArea(float leg1, float leg2);
     20 
     21 /* Main Function */
     22 int main()
     23 {       
     24         /* InitIalizers for legs and hypotenuse */ 
     25         float leg1 = -1.0, leg2 = -1.0, hypotenuse = -1.0;
     26         
     27         /* Sentinal Controlled loop initializers */
     28         int selection = 0;
     29         int EXIT_VAL = 10;
     30         
     31         /* Beginning of sentinal controlled loop */
     32         while(selection != EXIT_VAL)
     33         {       
     34                 /* Print Menu first */
     35                 printMenu();
     36                 scanf("%d", &selection);
     37                 
     38                 /* Switch on menu options */
     39                 switch(selection)
     40                 {
     41                         /* Input leg1 */
     42                         case '1':
     43                         {printf( "Enter leg1:");
     44                         scanf( "%f",&leg1 );
     45                         if ( leg1 > 0)
     46                         else printf( "Please enter a positive value for leg1\n");
     47                                 /* Did they enter a positive number? */
     48 
     49                                 break;
     50                         }
     51 
     52                         /* Input leg2 */
     53                         case '2':
     54                         {printf( "Enter leg2:");
     55                         scanf( "%f",&leg2 );
     56 
     57                         if ( leg2 > 0)
     58                         else printf("Please enter a positive number:";
     59                                 /* Did they enter a positive number? */
     60                                 break;
     61                         }
     62 
     63                         /* Input hypotenuse */
     64                         case '3':
     65                         {printf( "Enter hypotenuse:");
     66                         scanf( "%f",&hypotenuse );
     67                         if ( hypotenuse > 0)
     68                         printf( "Please enter a positive value for the hypotenuse\n");
     69                                 /* Did they enter a positive number? */
     70                                 break;
     71                         }
     72 
     73                         /* Calculate leg1 */
     74                         case '4':
     75                         {
     76                                 /* If enough values have been inputed to solve for it */
     77                          if leg1 = calLeg1(leg2, hypotenuse)
     78                          else printf("Not enough information:");
     79 
     80 
     81                                 break;
     82                         }
     83 
     84                         /* Calculate leg2 */
     85                         case '5':
     86                         {
     87                                 /* If enough values have been inputed to solve for it */
     88                         if leg2 = calLeg2(leg1, hypotenuse
     89                         else printf("Print need more information:");
     90                                 break;
     91                         }
     92 
     93                         /* Calculate hypotenuse */
     94                         case '6':
     95                         {
     96                                 /* If enough values have been inputed to solve for it */
     97                                 if hypotenuse = calHypotenuse(leg1, leg2)
     98                                 else printf("Need more information:");
     99                                 break;
    100                         }
    101 
    102                         /* Calculate area and print */
    103                         case '7':
    104                         { area = ( leg1 * leg2 )/2;
    105                         printf("The area is %f\n",area);
    106                                 /* If enough values have been inputed to solve for it
    107                                  * a little more complexe than the other calculates    */
    108                                 break;
    109                         }
    110 
    111                         /* Determine if values are a right triangle or not */
    112                         case '8':
    113                         { if ( leg 1 * leg1 ) + (leg2 * leg2) = ( hypotenuse * hypotenuse )
    114                         printf("This is a right triangle\n");
    115                         else printf("This is not a right triangle\n");
    116                                 /* Make sure you handle all possible cases */
    117                                 break;
    118                         }
    119 
    120                         case '9':
    121                         {printf( "The values are %f\n%f\n%f",leg1,leg2,hypotenuse);
    122                                 /* Display current values for leg1, leg2, and hypotenuse */
    123                                 break;
    124                         }
    125 
    126                         /* Exit Case */
    127                         case '10':
    128                         {
    129                                 break;
    130                         }
    131 
    132                         /* If invalid value entered */
    133                         default:
    134                         {
    135                                 printf("Invalid Value entered please select again!\n");
    136                         }
    137                 }
     138         return 0;
    139 }
    140 
    141 /* Menu Printing Function */
    142 void printMenu()
    143 {
    144 /* Do work for menu
    145  * 1.  Input value for leg1
    146  * 2.  Input value for leg2
    147  * 3.  Input value for hypotenuse
    148  * 4.  Calculate leg1
    149  * 5.  Calculate leg2
    150  * 6.  Calculate hypotenuse
    151  * 7.  Calculate area of right triangle
    152  * 8.  State if values entered is a real right triangle
    153  * 9.  Display current values of leg1, leg2, and hypotenuse
    154  * 10. Exit */
    155 }
    156 
    157 
    158  float calLeg1(float leg2, float hypotenuse)
    159 {
    160         /* Calculate value for leg1 and return it */
    161 }
    162 
    163  float calLeg2(float leg1, float hypotenuse)
    164 {
    165         /* Calculate value for leg2 and return it */
    166 }
    167 
    168  float calHypotenuse(float leg1, float leg2)
    169 {
    170         /* Calculate value for hypotenuse and return it */
    171 }
    172 
    173  float calArea(float leg1, float leg2)
    174 {
    175         /* Calculate area for triangle and return it */f}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They all mean what they say:
    righttriangle.c:45: error: expected expression before âelseâ
    You have to do something, anything, between the if (test) and the else.
    righttriangle.c:126:9: warning: multi-character character constant
    Characters, that is, things inside 'single quotes', can only be ... a character. So you can try to do '10', but that doesn't mean anything, because '10' is not a single character. You (apparently) want to be using integers anyway, so lose the quotes.
    righttriangle.c:175: error: âfâ undeclared (first use in this function)
    You've got an "f" sticking out at the end of your last comment. And a ")". And then the compiler complains, because you told it you were defining a function "calArea", but didn't do so.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    But I am doing something in line 45 I am saying if leg > 1 move onto the next case if its not print please enter a postive value.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Or atleast that's what I am trying to say.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you have your test backwards. Test for < 0, and don't have an else.

    (If you absolutely positively for whatever reason (which is probably not good) simply must have a "move on" statement, then you need to put in a do-nothing statement:
    Code:
    ; //do nothing
    But usually this means "Hey you coded your test backwards.")

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    case 1:
       printf("Enter leg1:");
       if (1 != scanf("%f", &leg1) || 0 >= leg1)
       {
          /* A valid entry was not provided */
          printf("ERROR: A positive value was not entered for leg1\n");
       }
       break;

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    area is undeclared! That means I need to declare it in main on line 105

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    What does control reaches end of non void function mean?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BSmith4740 View Post
    area is undeclared! That means I need to declare it in main on line 105
    What? I guess looking back that is true -- you need to declare that variable before you use it.

    Also, just skimming, you're missing a close parenthesis in your calculate leg-2 case; and I would guess you're missing some close braces due to all the "end of input reached" messages you get too.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What does this to do with c#?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM