Switch statement

This is a discussion on Switch statement within the C Programming forums, part of the General Programming Boards category; Code: /* Calculate leg1 */ 101 case 4: 102 { 103 104 /* If enough values have been inputed to ...

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

    Switch statement

    Code:
    /* Calculate leg1 */
    101                         case 4:
    102                         {
    103 
    104                         /* If enough values have been inputed to solve for it */
    105                         do{
    106                         (void float leg1 = calLeg1(leg2, hypotenuse));
    107                         
    108                         
    109                         }
    110                         while (  leg2 >= 0, hypotenuse >= 0 )
    111                                 break;
    112                         }
    106: error: two or more data types in declaration specifiers
    error: expected ‚)‚ before ‚leg1‚
    error: expected expression before ‚;‚ token
    warning: left-hand operand of comma expression has no effect
    error: expected ‚;‚ before ‚break‚

    What is this a)a before leg1 mean?
    What does a)a before break mean?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's not a)a. The funny symbols were supposed to be (are, in the right encoding, I suppose) quotes. The compiler expected a ")" before leg1, because it saw "(void float" and thought you were trying to do a cast. Maybe you are trying to do a cast, but I don't know what "void float" is supposed to mean.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Without knowing what your code is doing:
    Code:
    case 4:
    {
        /* If enough values have been inputed to solve for it */
        do
        {
            (void float leg1 = calLeg1(leg2, hypotenuse));
        } while (  leg2 >= 0 && hypotenuse >= 0 );
        break;
    }
    Remove bits in red, add bits in blue. That's just a guess however.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I am trying to write a program to see if a triangle is a right triangle using a switch statement using funcitons.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Quote Originally Posted by hk_mp5kpdw View Post
    Without knowing what your code is doing:
    Code:
    case 4:
    {
        /* If enough values have been inputed to solve for it */
        do
        {
            (void float leg1 = calLeg1(leg2, hypotenuse));
        } while (  leg2 >= 0 && hypotenuse >= 0 );
        break;
    }
    Remove bits in red, add bits in blue. That's just a guess however.

    Good guess, I tried it but it didn't work.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Code:
       
                          case 4:
    102                         {
    103 
    104                         /* If enough values have been inputed to solve for it */
    105                         do{
    106                         float leg1 = calLeg1(leg2, hypotenuse);
    107                         
    108 
    109 
    110                         
    111                         }
    112                         while ( leg1 >= 0 && leg2 >= 0 && hypotenuse >= 0 );
    113                         
    114                                 
    115                                 break;
    116                         }
    106: warning: unused variable âleg1â
    righttriangle.c:124: error: expected &#226â before âleg2â

    Not it says the variable isn't being used? I am calling a function. How is it not being used?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BSmith4740 View Post
    Good guess, I tried it but it didn't work.
    If I remember your program correctly, you already have a leg1 variable defined elsewhere. So don't define it again. (IOW, remove the "float" too.)

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I don't know how this going to come out but I am making progress. I am on the area now and playing with it.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    If you have a function for the area. Why would you have to give it a formula for the area that makes no sense?
    expected expression before âfloatâ
    righttriangle.c:142: error: too few arguments to function âcalAreaâ
    righttriangle.c:142: error: expected â;â before &#226â token
    righttriangle.c:142: error: expected statement before &#226â token
    righttriangle.c:147: error: âareaâ undeclared (first use in this function)
    righttriangle.c:147: error: (Each undeclared identifier is reported only once
    righttriangle.c:147: error: for each function it appears in.)

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BSmith4740 View Post
    If you have a function for the area. Why would you have to give it a formula for the area that makes no sense?
    You don't.
    Quote Originally Posted by BSmith4740 View Post
    expected expression before ‚float‚
    righttriangle.c:142: error: too few arguments to function ‚calArea‚
    righttriangle.c:142: error: expected ‚;‚ before ‚)‚ token
    righttriangle.c:142: error: expected statement before ‚)‚ token
    righttriangle.c:147: error: ‚area‚ undeclared (first use in this function)
    righttriangle.c:147: error: (Each undeclared identifier is reported only once
    righttriangle.c:147: error: for each function it appears in.)
    Make sure your function prototype (at the top), function call (in the program itself) and function definition (at the end) agree as to how many things need to get passed in to the function.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Code:
     
    
    case 7:
    140                         {
    141                         do { area = ( leg1 + leg2 )/2;
    142                          
    143                          calArea(float leg1, float leg2));
    144 
    145                         
    146                         }
    147                          while ( leg1 >= -1 && leg2 > -1);
    148                         printf("The area is equal to %f",area);

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Remember: when you call a function, you do NOT include data types.

    So why don't you just do
    Code:
    area = calArea(leg1, leg2);
    ?

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Why can't I just do area = ( leg1 * leg2 ) /2;

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    What does control reaches end of of non void function mean? I don't have main void. I didn't think I was supposed to have main void when using functions?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    What does control reaches end of of non void function mean? I don't have main void.
    It means that you have a function that returns a value, but you do not actually return a value. For example, you have int main(), but you did not return 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21