HELP - Stuck on some coding

Printable View

• 10-29-2009
trueman1991
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 :)
• 10-29-2009
bithub
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.
• 10-29-2009
trueman1991
yeah i found the rogue commas lol, and will try the block of if's :)
• 10-29-2009
trueman1991
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);
• 10-29-2009
trueman1991
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"
• 10-29-2009
rags_to_riches
Get rid of the semi-colons after the if() statements!
Code:

`if(width<0);`
And use code tags for Pete's sake!
• 10-29-2009
trueman1991
Thanks, worked a charm :)
and yeah will remember next time, sorry :P