Thread: Switch statement

  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,336
    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,817
    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,336
    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,336
    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,336
    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
    28,413
    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.
    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

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