Thread: HELP - Stuck on some coding

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    28

    Unhappy HELP - Stuck on some coding

    Im stuck on some if coding, i have done lots of if coding before and cant see why it doesnt work with this particular coding:

    36 float width ;
    37 float length ;
    38 float result;
    39
    40
    41 printf("Please Enter the width of the triangle: ");
    42 scanf("%f", &width);
    43 if(width<0);
    44 printf("The width cant hold any negative values, please re-enter data", );
    45 scanf("%f", &width);
    46 else
    47 printf("Please Enter the length of the side of the triangle: ");
    48 scanf("%f", &length);
    49 if(length<0);
    50 printf("The length cant hold any negative values, please re-enter data", );
    51 scanf("%f", &length);
    52 if(length<width);
    53 printf("The length is smaller than the width, please enter a new width",);
    54 scanf("%f", &length);
    55 else
    56 scanf("%f", &length);
    57 printf("The height of the traingle is: %.2f", sqrt(length*length+width*width));
    58 printf("\n");
    59 }



    44: error: expected primary-expression before ')' token
    46: error: expected primary-expression before "else"
    46: error: expected `;' before "else"
    50: error: expected primary-expression before ')' token
    53: error: expected primary-expression before ')' token
    55: error: expected primary-expression before "else"
    55: error: expected `;' before "else"


    Any help would be grateful

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    44 printf("The width cant hold any negative values, please re-enter data", );
    So what goes after the comma? Why is the comma there?
    Code:
    46 else
    What if does this else correspond to? The answer is none, and that's why you're getting an error here. Did you mean to put braces around your if block?
    Code:
    50 printf("The length cant hold any negative values, please re-enter data", );
    53 printf("The length is smaller than the width, please enter a new width",);
    More rogue commas.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    yeah i found the rogue commas lol, and will try the block of if's

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    still no luck - also im trying to do it all within one function
    This is what i have at the moment (if statement wise) and it still comes up with the errors about the else statements, i have tested the programme without the else statements and it works fine but obviously the else statements are key in this programme.

    if(width<0);
    printf ("The width cant hold any negative values, please re-enter data \n" );
    else
    printf("Please Enter the length of the side of the triangle: ");
    scanf("%f\n", &length);
    if(length<0);
    printf("The length cant hold any negative values, please re-enter data" );
    else
    scanf("%f\n", &length);

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    these are the errors im getting, and are both the same for both else statments

    51: error: expected primary-expression before "else"
    51: error: expected `;' before "else"

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Get rid of the semi-colons after the if() statements!
    Code:
    if(width<0);
    And use code tags for Pete's sake!

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    Thanks, worked a charm
    and yeah will remember next time, sorry :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM