# Thread: Helping out a friend...

1. ## 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 */

printf("Enter the radius. \n");

/* Compute the area */

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 */

printf(Enter the length of the radius. \n");

/* Compute the area */

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. 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. 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. 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)`

5. 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;`
Code:
`char shape, char rectangle;`
and it told me "parse error before 'char' " What does that mean? Which 'char' is it talking about?

6. Code:
`if (shape = rectangle)`
should probably be
Code:
`if (shape == rectangle)`

7. 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?

8. 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. 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.

10. 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. 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. 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. 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?

14. 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. 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