Thread: Fence Program - Help

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    7

    Unhappy 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. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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;	
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #define PI = 3.14159
    Try
    #define PI 3.14159

    > if (shape=1)
    Should be
    if ( shape == 1 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    7

    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. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    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.
    Last edited by nomi; 01-19-2004 at 06:35 PM.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #10
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    You still need to pause the programme.

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

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    7

    Smile 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. #12
    Registered User
    Join Date
    Jan 2004
    Posts
    14
    You could use a For loop....

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #14
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    What do you mean by OP?

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by nomi
    What do you mean by OP?
    Original Poster
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM