Thread: Switch statement

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    What does this mean?
    expected â;â before âprintfâ


    }
    152 while ( leg1 >= -1 && leg2 > -1 && area >= -1)
    153 printf("The area is equal to %f\n",area);

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BSmith4740 View Post
    What does this mean?
    expected â;â before âprintfâ


    }
    152 while ( leg1 >= -1 && leg2 > -1 && area >= -1)
    153 printf("The area is equal to %f\n",area);
    Think for six seconds: it tells you you need a semicolon before printf and the statement on the line before is mysteriously missing a semicolon....

  3. #18
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Yes I caught that one. you are right I need to stop and think.

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    while ( calHypotenuse * calHypotenuse == calLeg1 * calLeg1 + calLeg2 * calLeg2);
    173 while ( hypontenuse >= -1 && leg1 >= -1, and && leg2 >= -1);
    Why can't I do this?
    It says invalid operands to binary? I am guessing I have to use the pow function. I am trying to find some examples of it online but they aren't making much since.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have the literal characters ", and " in your code? We're not trying to write grammatical English here (merits of the Oxford comma aside).

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I had a similiar statement in my before cases and the compiler was passing them so I thought I had to have it here. Its freaking out on my latest pow attempt.

    float pow (float calHypotenuse, float 2) == float pow (float calLeg1, float 2) + float pow (float calLeg2, float2);
    172 printf("This is a right triangle\n");
    173

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BSmith4740 View Post
    I had a similiar statement in my before cases and the compiler was passing them so I thought I had to have it here. Its freaking out on my latest pow attempt.

    float pow (float calHypotenuse, float 2) == float pow (float calLeg1, float 2) + float pow (float calLeg2, float2);
    172 printf("This is a right triangle\n");
    173
    Look at this:
    Code:
    hypotenuse = sqrt(pow(calLeg1, 2)+pow(calLeg2, 2));
    Note that you still do not use types when passing arguments to functions and that assignment is =, not == (which is comparison for equality), and that the left side of an assignment must be a variable that can be assigned to.

    Do you have a textbook? Can you find a textbook online? At least look at what actual C code looks like, and try to emulate it.

  8. #23
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    169 /* float pow ( float base, float exponent ); */
    170
    171
    172 while ( calHypotenuse * calHypotenuse == calLeg1 * calLeg1 + calLeg2 * calLeg2);
    173 printf("This is a right triangle\n");
    174 printf("This is not a right triangle\n");
    175 while ( calHypotenuse * calHypotenuse != calLeg1 * calLeg1 + calLeg2 * calLeg2);
    176
    177
    178
    179 }
    180

    172: error: invalid operands to binary *
    righttriangle.c:172: error: invalid operands to binary *
    righttriangle.c:172: error: invalid operands to binary *
    righttriangle.c:176: warning: comparison between pointer and integer
    righttriangle.c:176: warning: comparison between pointer and integer
    righttriangle.c:176: error: âandâ undeclared (first use in this function)
    righttriangle.c:176: error: (Each undeclared identifier is reported only once
    righttriangle.c:176: error: for each function it appears in.)
    righttriangle.c:176: warning: comparison between pointer and integer
    righttriangle.c:176: warning: left-hand operand of comma expression has no effect
    righttriangle.c:179: error: invalid operands to binary *
    righttriangle.c:179: error: invalid operands to binary *
    righttriangle.c:179: error: invalid operands to binary *
    righttriangle.c:187: warning: too many arguments for format
    righttriangle.c: In function âcalLeg1â:
    righttriangle.c:229: warning: control reaches end of non-void function
    righttriangle.c: In function âcalLeg2â:
    righttriangle.c:234: warning: control reaches end of non-void function
    righttriangle.c: In function âcalHypotenuseâ:
    righttriangle.c:239: warning: control reaches end of non-void function
    righttriangle.c: In function âcalAreaâ:
    righttriangle.c:244: warning: control reaches end of non-void function

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I got a little inconsistent there because I forgot your naming conventions: you need to use the variables everywhere (hypotenuse, leg1, leg2) if you want their values. You only call the function when you want that value to be calculated.

  10. #25
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    http://www.cplusplus.com/reference/c...cmath/pow.html

    This is what I was looking at for pow. I am doing the best I can do tabstop. This program is simple and its taking me three days.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BSmith4740 View Post
    http://www.cplusplus.com/reference/c...cmath/pow.html

    This is what I was looking at for pow. I am doing the best I can do tabstop. This program is simple and its taking me three days.
    And which part of
    pow(7,3)
    or
    pow(32.01,1.54)
    led you to
    float pow (float calLeg1, float 2)
    ? There's nothing wrong with looking at examples, but you do have to actually look at them.

  12. #27
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    float pow ( float base, float exponent );

    this did tab stop.

    Why is this returning non void functions?

    Code:
    #include <stdio.h>
     11 #include <math.h>
     12 
     13 /* Function Prototypes */
     14 void printMenu();
     15 float calLeg1(float leg2, float hypotenuse);
     16 float calLeg2(float leg1, float hypotenuse);
     17 float calHypotenuse(float leg1, float leg2);
     18 float calArea(float leg1, float leg2); 
     19 
     20 /* Main Function */
     21 int main()
     22 {
     23         /* Initializers for legs and hypotenuse */
     24         float leg1 = -1.0, leg2 = -1.0, hypotenuse = -1.0, area = -1;
     25 
     26 
     27 
     28 
     29         /* Sentinal Controlled loop initializers */
     30         int selection = 0;
     31         int EXIT_VAL = 10;
     32 
     33         /* Beginning of sentinal controlled loop */
     34         while(selection != EXIT_VAL)
     35         {
     36                 /* Print Menu first */
     37                 printMenu();
     38                 scanf("%d", &selection);
     39 
     40                 /* Switch on menu options */
     41                 switch(selection)
     42                 {
     43                         /* Input leg1 */
     44                         case '1' :
     45                         {
     46                         do{
     47                         printf("Enter leg1\n");
     48 
     49                         }
     50                         while (leg1 <= 0);
     51                         printf("Please enter a positive number:");
     52                         while (leg1 >= 0);
     53                         break;
     54                 }
     55 
     56 
     57 
     58 
     59 
     60 
     61 
     62                                 /* Did they enter a positive number? */
     63                                 break;
     64 
     65 
     66                         /* Input leg2 */
     67                         case '2' :
     68                         {
     69                         do{
     70                         printf("Enter leg2\n");
     71                         scanf("%f",&leg2);
     72                         }
     73                         while ( leg2 <= 0);
     74                         printf("Please enter a positive numbers");
     75                         while ( leg2 >= 0);
     76 
     77                                 /* Did they enter a positive number? */
     78                         }
     79 
     80                         /* Input hypotenuse */
     81                         case 3:
     82                         {
     83                         do{
     84                         printf("Enter hypotenuse:");
     85                         scanf("%f",&hypotenuse);
     86                         }
     87                         while (hypotenuse <= -1);
     88                         printf("Please enter a positive number dummy:");
     89                         while (hypotenuse >= -1);
     90                         break;
     91 
     92 
     93 
     94 
     95 
     96                         /* Did they enter a positive number? */
     97                                 break;
     98                         }
     99 
                                                                  99,0-1        33%
     61 
     62                                 /* Did they enter a positive number? */
     63                                 break;
     64 
     65 
     66                         /* Input leg2 */
     67                         case '2' :
     68                         {
     69                         do{
     70                         printf("Enter leg2\n");
     71                         scanf("%f",&leg2);
     72                         }
     73                         while ( leg2 <= 0);
     74                         printf("Please enter a positive numbers");
     75                         while ( leg2 >= 0);
     76 
     77                                 /* Did they enter a positive number? */
     78                         }
     79 
     80                         /* Input hypotenuse */
     81                         case 3:
     82                         {
     83                         do{
     84                         printf("Enter hypotenuse:");
     85                         scanf("%f",&hypotenuse);
     86                         }
     87                         while (hypotenuse <= -1);
     88                         printf("Please enter a positive number dummy:");
     89                         while (hypotenuse >= -1);
     90                         break;
     91 
     92 
     93 
     94 
     95 
     96                         /* Did they enter a positive number? */
     97                                 break;
     98                         }
     99 
    100                         /* Calculate leg1 */
    101                         case 4:
    102                         {
    103 
    104                         /* If enough values have been inputed to solve for it */
    105                         do{
    106                         leg1 = calLeg1(leg2, hypotenuse);
    107 
    108 
    109 
    110 
    111 
    112                         }
    113                         while ( leg1 >= -1 && leg2 >= -1 && hypotenuse >= -1 );
    114 
    115 
    116 
    117                                 break;
    118                         }
    119 
    120                         /* Calculate leg2 */
    121                         case 5:
    122                         {
    123                                 /* If enough values have been inputed to solve for it */
    124                         do{
    125                         leg2 = calLeg2(leg1, hypotenuse);
    126                         }
    127                         while ( leg1 >= -1 && leg2 >= -1 && hypotenuse >= -1);
    128 
    129 
    130 
    131                         break;
    132                         }
    133 
    134 
    135 
    136 
    137 
    138                         /* Calculate area and print */
    139                         case 7:
    140                         {
    141                         do {
    142                         area = calArea(leg1, leg2);
    143 
    144 
    145 
    146 
    147 
    148 
    149 
    150 
    151                         }
    152                          while ( leg1 >= -1 && leg2 > -1 && area >= -1);
    153                         printf("The area is equal to %f\n",area);
    154 
    155 
    156                         break;
    157                         }
    158 
    159                         /* If enough values have been inputed to solve for it
    160                                  * a little more complexe than the other calculates    */
    161 
    162 
    163 
    164 
    165                         /* Determine if values are a right triangle or not */
    166                         case 8:
    167                         {
    168 
    169                         /*    float pow (       float base,       float exponent );  */
    170 
    171 
    172                         while (hypotenuse = sqrt(pow(calLeg1, 2)+pow(calLeg2, 2)));
    173                         printf("This is a right triangle\n");
    174                         while (hypotenuse != sqrt(pow(calLeg1, 2)+pow(calLeg2, 2)));
    175                         printf("This is not a right triangle\n");
    176                                 /* Make sure you handle all possible cases */
    177                                 break;
    178                         }
    179 
    180                         case 9:
    181                         {
    182                         printf("THe values for leg1,leg2, and hypotenuse\n",leg1,leg2,hypotenuse);
    183 
    184                         break;}
    185 
    186                                 /* Display current values for leg1, leg2, and hypotenuse */
    187 
    188 
    189 
    190                         /* Exit Case */
    191                         case 10:
     40                 /* Switch on menu options */
     41                 switch(selection)
     42                 {
     43                         /* Input leg1 */
     44                         case '1' :
     45                         {
     46                         do{
     47                         printf("Enter leg1\n");
     48 
     49                         }
     50                         while (leg1 <= 0);
     51                         printf("Please enter a positive number:");
     52                         while (leg1 >= 0);
     53                         return leg1;
     54                         break;
     55                 }
     56 
     57 
     58 
     59 
     60 
     61 
     62                                 
     63                                 /* Did they enter a positive number? */
     64                                 break;
     65 
     66                         
     67                         /* Input leg2 */
     68                         case '2' :
     69                         {
     70                         do{
     71                         printf("Enter leg2\n");
     72                         scanf("%f",&leg2);
     73                         }
     74                         while ( leg2 <= 0);
     75                         printf("Please enter a positive numbers");
     76                         while ( leg2 >= 0);
     77                         return leg2;
     78                         break;  
     79                                 
     80                                 /* Did they enter a positive number? */
     81                         }
     82                         
     83                         /* Input hypotenuse */
     84                         case 3:
     85                         {
     86                         do{
     87                         printf("Enter hypotenuse:");
     88                         scanf("%f",&hypotenuse);
     89                         }
     90                         while (hypotenuse <= -1);
     91                         printf("Please enter a positive number dummy:");
     92                         while (hypotenuse >= -1);
     93                         return hypotenuse;
     94                         break;
     95 
     96 
     97 
     98 
     99 
    100                         /* Did they enter a positive number? */
    101                                 break;
    102                         }
    103 
    104                         /* Calculate leg1 */
    105                         case 4:
    106                         {
    107 
    108                         /* If enough values have been inputed to solve for it */
    109                         do{
    110                         leg1 = calLeg1(leg2, hypotenuse);
    111 
    112 
    113 
    114 
    115 
    116                         }
    117                         while ( leg1 >= -1 && leg2 >= -1 && hypotenuse >= -1 );
    118 
    119 
    120                         return leg1;
    121                                 break;
    122                         }
    123 
    124                         /* Calculate leg2 */
    125                         case 5:
    126                         {
    127                                 /* If enough values have been inputed to solve for it */
    128                         do{
    129                         leg2 = calLeg2(leg1, hypotenuse);
    130                         }
    131                         while ( leg1 >= -1 && leg2 >= -1 && hypotenuse >= -1);
    132 
    133 
    134                         return leg2;
    135                         break;
    136                         }
    137 
    138 
    139 
    140 
    141 
    142                         /* Calculate area and print */
    143                         case 7:
    144                         {
    145                         do {
    146                         area = calArea(leg1, leg2);
    147 
    148 
    149 
    150 
    151 
    152 
    153 
    154 
    155                         }
    156                          while ( leg1 >= -1 && leg2 > -1 && area >= -1);
    157                         printf("The area is equal to %f\n",area);
    158                         return area;
    159                 
    160                         break;
    161                         }
    162                         
    163                         /* If enough values have been inputed to solve for it
    164                                  * a little more complexe than the other calculates    */
    165 
    166 
    167 
    168 
    169                         /* Determine if values are a right triangle or not */
    170                         case 8:
    171                         {
    172 
    173                         /*    float pow (       float base,       float exponent );  */
    174 
    175 
    176                         while (hypotenuse = sqrt(pow(calLeg1, 2)+pow(calLeg2, 2)));
    177                         printf("This is a right triangle\n");
    178                         while (hypotenuse != sqrt(pow(calLeg1, 2)+pow(calLeg2, 2)));
    179                         printf("This is not a right triangle\n");
    180                         return hypotenuse;
    181                                 /* Make sure you handle all possible cases */
    182                                 break;
    183                         }
    184                         
    185                         case 9:
    186                         {
    187                         printf("THe values for leg1,leg2, and hypotenuse\n",leg1,leg2,hypotenuse);
    188 
    189                         break;}
    190 
    191                                 /* Display current values for leg1, leg2, and hypotenuse */
    192 
    193 
    194 
    195                         /* Exit Case */
    196                         case 10:
    197                         {
    198 
    199                         /* If invalid value entered */
    200                         default:
    201 
    202                                 printf("Invalid Value entered please select again!\n");
    203                                 return default;
    204                         break;
    205                         }
    206         
    207         return 0;
    208 
    209 }
    righttriangle.c:172: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:172: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:172: warning: suggest parentheses around assignment used as truth value
    righttriangle.c:174: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:174: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:182: warning: too many arguments for format
    righttriangle.c:201: error: âhypontenuseâ undeclared (first use in this function)
    righttriangle.c:201: error: (Each undeclared identifier is reported only once
    righttriangle.c:201: error: for each function it appears in.)
    righttriangle.c:201: warning: left-hand operand of comma expression has no effect
    righttriangle.c:209:1: warning: "/*" within comment
    righttriangle.c:241:4: warning: "/*" within comment
    righttriangle.c:206:1: error: unterminated comment
    righttriangle.c:203: error: expected declaration or statement at end of input
    righttriangle.c:203: error: expected declaration or statement at end of input
    righttriangle.c:203: warning: control reaches end of non-void function

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BSmith4740 View Post
    float pow ( float base, float exponent );

    this did tab stop.
    And, if you read the page, you will notice that this is not how the function is called. This is the function prototype for the function, telling you that it takes two floats and returns a float result. Again: read the example.
    righttriangle.c:172: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:172: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:172: warning: suggest parentheses around assignment used as truth value
    righttriangle.c:174: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:174: error: incompatible type for argument 1 of âpowâ
    righttriangle.c:182: warning: too many arguments for format
    righttriangle.c:201: error: âhypontenuseâ undeclared (first use in this function)
    righttriangle.c:201: error: (Each undeclared identifier is reported only once
    righttriangle.c:201: error: for each function it appears in.)
    righttriangle.c:201: warning: left-hand operand of comma expression has no effect
    righttriangle.c:209:1: warning: "/*" within comment
    righttriangle.c:241:4: warning: "/*" within comment
    righttriangle.c:206:1: error: unterminated comment
    righttriangle.c:203: error: expected declaration or statement at end of input
    righttriangle.c:203: error: expected declaration or statement at end of input
    righttriangle.c:203: warning: control reaches end of non-void function
    The "control reaches end of non-void function" means that you have "run off the end of the page" -- your source code file stops, but your function (in this case, I think, main) isn't done yet. Non-void functions don't end until you get to a "return" statement.

  14. #29
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    but I am returning something. I went into each case and told it to return either the hypotenuse, area etc. It won't go away.

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's hard to tell without looking at your source code, but something is getting through. Also those "unterminated comment" errors are worrisome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM