# Thread: Fence Program - Help

1. ## Fence Program - Help

Hello. I am working on an assignment for class, and I was wondering if I am following flow of control correctly here. I'm still a litle new to flow of control and using the if, else, and while statments.

Any help would be greatly appreciated! Thanks.
Gabe

Code:
```#include <stdio.h>
#define PI = 3.14159

int main(void)

{
int c, r, shape;
float a;

printf("What is the length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0);
printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
if (shape!=1 || shape!= 2);
printf("Sorry, that is an invalid shape.\n");
else
if (shape=1);
r=( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);

if (shape=2);
r=( 4 * c );
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);

else
printf("Sorry. The length of the fence must be a positive integer.\n");

return 0;
}```

2. You need to enclose the contents of an if statement in braces { }.

Code:
```#include <stdio.h>
#define PI = 3.14159

int main(void)

{
int c, r, shape;
float a;

printf("What is the length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0)
{
printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
if (shape!=1 || shape!= 2)
printf("Sorry, that is an invalid shape.\n");
else
{
if (shape=1)
{
r=( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);
}

if (shape=2)
{
r=( 4 * c );
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);
}
}
}

else
printf("Sorry. The length of the fence must be a positive integer.\n");

return 0;
}```

3. Not at all.
If statements are designed
Code:
```if (x == y)  // no ';'
{
code if true
}
else
{
code if false
}```
Try again.

Oh, and for your first post, using code tags is a GREAT start! Thanks for making it easy for us to read your code! Wish more would follow the rules.

4. > #define PI = 3.14159
Try
#define PI 3.14159

> if (shape=1)
Should be
if ( shape == 1 )

5. As a point of style I would ask the shape first so you can ask a more detailed question for the length. IE: "What is the length of one side of the fence."
You've got the idea right, just fix the points mentioned and you'll be good.
Just a quick explanation of the purpose of the &#123 and &#125. The if and else statements only include the command immediatly following them. But by using the &#123 and &#125 you are creating a block of commands that will be treated as one command in the eyes of if and else.

6. ## Thank you! =o)

Thank you very much to the 3 of you for helping me out! =o) I didn't think I'd get such fast replies...lol.

That helped reduce the errors to only two, and I'm trying to put parantheses and brackets in differetn places, but I'm still not getting where my errors are coming up before "else":

In function `main':
fence.c:32: parse error before `else'
At top level:
fence.c:45: parse error before `else'

here's my code:

Code:
```#include <stdio.h>
#define PI 3.14159

int main(void)

{
int c, r, shape;
float a;

printf("What is the length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0);
{	printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
if (shape!=1 || shape!= 2);
printf("Sorry, that is an invalid shape.\n");
else
{	if (shape==1);
{	r=( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);
}
if (shape==2);
{	r=( 4 * c );
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);
}
}
}
else
printf("Sorry. The length of the fence must be a positive integer.\n");

return 0;
}```

7. You have two "if" statements that have semi-colons on the end of their lines:

>>if (c >= 0.0);

>>if (shape != 1 || shape != 2);

The semi-colon isn't needed.

Also, try not to mix the use of braces and no-braces for code blocks after an if statement, it makes things harder to read, imho. Just because there's only one line of code following an "if" statement doesn't mean you shouldn't use braces around it.

8. Fixed your parenthesis:

Code:
```#include <stdio.h>
#define PI 3.14159

int main(void)
{
int c, r, shape;
float a;

printf("What is the length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0){
printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
if (shape!=1 || shape!= 2){
printf("Sorry, that is an invalid shape.\n");
}
else{
if (shape==1){
r = ( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);
}
if (shape==2){
r=( 4 * c );
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);
}
}
}
else {
printf("Sorry. The length of the fence must be a positive integer.\n");
}

while(getchar () != '\n');
getchar ();
return 0;
}```
....and paused your program, but you need to fix your programme, It doesnt do what its supposed to do.

9. Here you go. My changes are in bold.
Code:
```#include <stdio.h>
#define PI 3.14159
int main(void)
{
int c, r, shape;
float a;

printf("What is the total length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0)
{
printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
printf("%d\n", shape);
if (shape!=1 && shape!= 2)
{
printf("Sorry, that is an invalid shape.\n");
}
else
{
if (shape==1)
{
r=( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);
}
if (shape==2)
{
r=( c / 4 ); /*Math error.  If the user entered the total length then it should be : c/4. */
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);
}
}
}
else
{
printf("Sorry. The length of the fence must be a positive integer.\n");
}

return 0;
}```
Also removed the ; after the if statments.

10. You still need to pause the programme.

.
.
.
Code:
```  while(getchar () != '\n');
getchar ();
return 0;```

11. ## SOOO CLOSE! lol

OK, I'm soooo close!!! LoL. =o)

It's printing everything correctly, but I am wondering 2 things:

1.) How can I make the program only print the Area of the Circle if 1 is selected, and only the Area of the Square when 2 is selected? (Now they are both printing).

2.) How can I make the program to continue to prompt the user to input after the error is printed (For a negative length)?

I'm really scared my flow is compltely off...lol.

Any help is very much appreciated! Sorry if I'm being dumb...I'll learn soon! =o)

Thanks!
-gabe

Code:
```#include <stdio.h>
#define PI 3.14159

int main(void)

{
int c, r, shape;
float a;

printf("What is the length of your fence in feet?\n");
scanf("%d", &c);
if (c >= 0.0)
{	printf("What shape will your enclosure be, (1)circle, or (2)square?\n");
scanf("%d", &shape);
if ((shape < 1) || (shape > 2))
{  printf("Sorry, that is not a valid shape.\n");
}
else
if (shape==1);
{	r=( c/(2 * PI) );
a=( PI * ( r * r ) );
printf("The area of your enclosure is %.2f.\n", a);
}
if (shape==2);
{	r=( 4 * c );
a=( r * r );
printf("The area of your enclosure is %.2f.\n", a);
}
}
if ( c < 0.0)
{	printf("Sorry. The length of the fence must be a positive integer.\n");
}
return 0;
}```

12. You could use a For loop....

13. nomi pausing the program isn't a requirement. Since the OP hasn't specified that problem there is no need for it.

ga836044: As stated before you are still putting a ; after the if statements. Get rid of them and it will work.
if (shape==1); should be if(shape==1) (same for the shape==2)

if you want to the user to only enter acceptable values you can do it with the following:
Code:
```do
{
printf("What is the total length of your fence in feet?\n");
scanf("%d", &c);
}while (c <= 0);```
Use the same method for the shape.
Code:
```do
{
/* Prompt */
/* Get values */
}while ( /* condition to test */ );```

14. What do you mean by OP?

15. Originally posted by nomi
What do you mean by OP?
Original Poster

Popular pages Recent additions