Thread: Helping out a friend...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    Helping out a friend...

    Ok... I've never programmed in C before, but my friend is taking a C course and I'm pretty good with computers and stuff and I learn pretty fast, so he asked me to help him. I got his assignment and his textbook and set out to learn C (or at least enough to do this basic assignment). He's supposed to make a program that computes the area of 5 different shapes. So here's what I've got so far...

    Code:
    /*This program will compute the area of a rectangle,                        */
    /*triangle, circle, ellipse, and sphere.                                    */
    
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    
    int main(void)
    {
       /* Declare first variable. */
    	
    	 char shape
    	
    	/* Determine which shape to find the area of */
      
       printf("Find the area of a rectangle, triangle, circle, ellipse, or sphere?");
    	scanf("%c",&shape);
    	
    	if (shape = rectangle)
    	   
    		/* Declare set of variables for this shape */
    		
    		float lengthRect, widthRect, areaRect;
    		       printf("Enter the length. \n");
    				 scanf("%lf",&lengthRect);
    				 printf("Enter the width. \n");
    				 scanf("%lf",&widthRect);
    		      
    				 /* Compute the area */
    		  
    		       areaRect = lengthRect*widthRect
    				 printf("The area of the rectangle is "
    				        "%5.2lf \n",areaRect);
    	else
    	{
    	   if (shape = triangle)
    	
    		   /* Declare set of variables for this shape */
    	
    			float baseTri, heightTri, areaTri;
    			       printf("Enter the base. \n");
    					 scanf("%lf",&baseTri);
    					 printf(Enter the height. \n");
    					 scanf("%lf",&heightTri);
    	
    					 /* Compute the area */
    	
    					 areaTri = .5(baseTri*heightTri)
    					 printf("The area of the triangle is "
    					        "%5.2lf \n",areaTri);
    		else
    		{
    	      if (shape = circle)
    	
    		   /* Declare set of variables for this shape */
    	
    			float radiusCirc, areaCirc;
    			       printf("Enter the radius. \n");
    					 scanf("%lf",&radiusCirc);
    	
    					 /* Compute the area */
    	
    					 areaCirc = PI*(radiusCirc*radiusCirc)
    					 printf("The area of the circle is "
    					        "%5.2lf \n",areaCirc);
                  else
                  {
                     if (shape = ellipse)
    		
    		           /* Declare set of variables for this shape */
    		
    			       float semiaxisA, semiaxisB, areaEllipse;
    			              printf("Enter the length of semiaxisA. \n");
    					 		  scanf("%lf",&semisacisA);
    					        printf(Enter the length of semiaxisB. \n");
    					        scanf("%lf",&semiaxisB);
    					       
    							  /* Compute the area */
    							  
    					        areaEllipse = PI*semiaxisA*semiaxisB
    					        printf("The area of the ellipse is "
    					        "%5.2lf \n",areaEllipse);
                      else
                      {
    						   if (shape = sphere)
    							
    							/* Declare set of variables for this shape */
    							
    							float radiusSphere, areaSphere;
    							       printf(Enter the length of the radius. \n");
    									 scanf("%lf",&radiusSphere);
    									 
    									 /* Compute the area */
    									 
    									 areaSphere = 4*PI*radiusSphere*radiusSphere
    									 printf("The surface area of the sphere is "
    									        "%5.2lf \n",areaSphere);
    			          }
    					 }
    			    }
    			 }
    	    return 0;
    	    }
    The first error message I get when I try to compile and link it is "syntax error before 'printf' " on the line

    Code:
       printf("Find the area of a rectangle, triangle, circle, ellipse, or sphere?");
    Why would it be telling me this? I can't figure it out. But I guess yo knew that or I wouldn't be posting here. Thanks for the help.

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    You always put ; at the end of evry statement

    this doesnt
    Code:
    char shape
       printf("Find the area of a rectangle, triangle, circle, ellipse, or sphere?");
    	scanf("%c",&shape);

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Aha. Thank you very much. I may just get this resolved tonight. Next question...

    It says " 'rectangle' undeclared (first use in this function)" in reference to

    Code:
    	if (shape = rectangle)
    I understand that this means that I haven't declared 'rectangle' as a variable anywhere in the program yet, but I didn't think I needed to. What should I do about that?

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    rectangle, circle, square etc etc... are not declared, you need to declare them and assign a unique character value in each of them.
    Code:
    	if (shape = rectangle)
    Last edited by loko; 09-15-2005 at 07:29 PM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    That didn't work. Or at least the way that I understood it, didn't work. What I did was, where I had
    Code:
    char shape;
    I added
    Code:
    char shape, char rectangle;
    and it told me "parse error before 'char' " What does that mean? Which 'char' is it talking about?
    Last edited by rhymewithnothin; 09-15-2005 at 07:23 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Code:
    if (shape = rectangle)
    should probably be
    Code:
    if (shape == rectangle)

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Bithub, that makes sense, I remember seeing in the textbook some double =, but I didn't know what they meant and I couldn't find in the book where they explained them. But they were in the section on "if/else" statements. I'll give that a shot.

    Changing the = to == didn't change the problems... any other suggestions?
    Last edited by rhymewithnothin; 09-15-2005 at 07:31 PM.

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    char shape, rectangle;  // correct way to declare variables
    and read on this Declaring a variable

    the == operator is use for comparing 2 values
    while = operator is use for assigning a value to a variable.

    Code:
    if (shape == rectangle) // so this is correct coz your comparing

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Gotcha. Let me take another shot at this...

    That definitely worked, you're a genius. I'll fix all that stuff in the program and get back to you. Thanks for all the help so far.
    Last edited by rhymewithnothin; 09-15-2005 at 07:40 PM.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Ok next error is... "parse error before 'else' "

    I won't specify a line of code because it says this for every 'else' statement I made. What did I do wrong?

  11. #11
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Right way to use if/else/else if structure

    Code:
    if( c == 'c' ){
    // doo something
    }
    else if( c == 'b' ){
      // do something
    }
    else if( c== 'd' ) {
     // do something
    }
    else {
    // if c is not 'c' or 'b' or 'd' it goes here
     // do something
    }

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    An else can only be part of an if, but you are doing
    Code:
    if (foo)
        bar();
        baz();
        quux();
    else
        bling();
    Because you have multiple statements, you need a block:
    Code:
    if (foo)
    {
        bar();
        baz();
        quux();
    } else
        bling();

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    Loko, you fixed it again. We're nearing the end, 5 more errors, and I may be able to get some of them myself.

    Next one....

    "called object is not a function" referring to
    Code:
    		 areaTri = (1/2)(baseTri*heightTri);
    OK I fixed the rest except that one and one more, so I'll add it in to this one to see if I can't get it fixed faster.

    It says "warning: no newline at end of file" What does that mean?
    Last edited by rhymewithnothin; 09-15-2005 at 08:30 PM.

  14. #14
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If your intention is to multiply (1/2) by (baseTri*heightTri) then you need the * operator:

    (1/2)*(baseTri*heightTri);

    But this alone won't work, because 1/2 will evaluate to 0. Use 1/2.0 or 0.5.

    Edit:

    no newline at end of file means there is no newline at the end of the file. Gee.

    Go to the end of your file, hit enter.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Quote Originally Posted by rhymewithnothin
    Loko, you fixed it again. We're nearing the end, 5 more errors, and I may be able to get some of them myself.

    Next one....

    "called object is not a function" referring to
    Code:
    		 areaTri = (1/2)(baseTri*heightTri);
    Code:
    areaTri = ((double)(1/2))*(baseTri*heightTri);
    I didn't read where you declared areaTri, but I assumed it is of type double.

    or

    Code:
    areaTri = (1/2.0)*(baseTri*heightTri);
    will also work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  2. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  3. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 04:24 PM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM