Helping out a friend...

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-15-2005
rhymewithnothin
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.
• 09-15-2005
loko
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);```
• 09-15-2005
rhymewithnothin
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?
• 09-15-2005
loko
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)`
• 09-15-2005
rhymewithnothin
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?
• 09-15-2005
bithub
Code:

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

`if (shape == rectangle)`
• 09-15-2005
rhymewithnothin
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?
• 09-15-2005
loko
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`
• 09-15-2005
rhymewithnothin
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.
• 09-15-2005
rhymewithnothin
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?
• 09-15-2005
loko
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 }```
• 09-15-2005
cwr
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();```
• 09-15-2005
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);`
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?
• 09-15-2005
cwr
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.
• 09-15-2005
jcarouth
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last