Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

lil help please

Name: Anonymous 2007-10-24 15:28

Can anyone tell me why this is not working AGH?


/* This program takes information from a file and reads it and calculates volumes of wood*/

#include <stdio.h>
#include <math.h>
#define FILENAME "wood.txt"
#define PI 3.14159


int main(void)
{
      
      
    /*Declare Variables*/
    int num_data_pts=0;
    char type;
    double sum_O=0, sum_C=0, sum_P=0, vol_O=0, vol_C=0, vol_P=0, D_in, L_ft, OD_in, OL_ft, PD_in, PL_ft, CD_in, CL_ft;
        FILE *wood;
      

      
    /*Open file*/
      
    wood = fopen(FILENAME, "r");
                if (wood == NULL)
                        printf("Error opening input file.  \n");
                else
                {
                        /*Read and accumulate information*/
        while ((fscanf(wood, "%c %lf %lf", &type, &D_in, &L_ft)) == 1)
        {
                num_data_pts++;

                switch (type)
                {
                case 'O': case 'o':
                        {
                        OD_in+=D_in;
                        OL_ft+=L_ft;
                vol_O=(PI*(OD_in*OD_in)*OL_ft/48);
                        }
                        break;

                case 'C': case 'c':
                        {
                        CD_in+=D_in;
                        CL_ft+=L_ft;
                vol_C=(PI*(CD_in*CD_in)*CL_ft/48);
                        }
                        break;

                case 'P': case 'p':
                        {
                        PD_in+=D_in;
                        PL_ft+=L_ft;
                vol_P=(PI*(PD_in*PD_in)*PL_ft/48);
                        }
                        break;


                }

                /* Print information*/

                printf("Number of logs processed: %d \n", num_data_pts);
                printf("Volume of Oak: %10.2lf \n", vol_O);
                printf("Volume of Cherry: %10.2 \n", vol_C);
                printf("Volume of Pine: %10.2 \n", vol_P);
        }
                }
               
        /*Exit Program */
        return 0;
}

Name: Anonymous 2007-10-24 15:58

Because God hates the way you indent.

Name: Anonymous 2007-10-24 16:32

lrn2fscanf

Name: Anonymous 2007-10-24 16:54

I have wood in my pants

Name: Anonymous 2007-10-24 17:19

For starters, lrn2function.

Name: Anonymous 2007-10-24 17:38

Identifying wood?

yep it's wood

Name: Anonymous 2007-10-24 18:57

/* This program takes information from a file and reads it and fails massively

   OP:

   - Learn to write meaningful comments.
   - Learn NOT to write dumbfuckerious comments. I mean ``Print
     information'' followed by four print statements or ``Exit
     Program'' followed by a return statement. Is your head populated
     by maggots or what?
   - Get your ass out of the middle ages and start using the metric
     system like the rest of the world.
   - Learn NOT to use unnecessary variables.
   - You imported math.h and still decided to define pi by your
     own. What's wrong with M_PI? Too PRECISE for you?
   - This program may not work either, but have fun fixing it, idiot.

*/

#include <stdio.h>
#include <math.h>

#define FILENAME "wood.txt"

double volume(double D_in, double L_ft)
{
    return M_PI * (D_in*D_in) * L_ft / 48;
}

int main(void)
{
    int num_data_pts=0;
    FILE *wood;

    double OD_in=0.0, OL_ft=0.0;
    double CD_in=0.0, CL_ft=0.0;
    double PD_in=0.0, PL_ft=0.0;

    double D_in, L_ft;
    char type;

    if ((wood = fopen(FILENAME, "r")) == NULL) {
        printf("Error opening input file.\n");
        return 1;
    }

    while (fscanf(wood, "%c %lf %lf", &type, &D_in, &L_ft) == 1)
    {
        num_data_pts++;

        switch (type)
        {
        case 'O': case 'o':
            OD_in += D_in;
            OL_ft += L_ft;
            break;

        case 'C': case 'c':
            CD_in += D_in;
            CL_ft += L_ft;
            break;

        case 'P': case 'p':
            PD_in += D_in;
            PL_ft += L_ft;
            break;
        }

        printf("Number of logs processed: %d\n", num_data_pts);
        printf("Volume of Oak: %10.2lf\n",  volume(OD_in, OL_ft));
        printf("Volume of Cherry: %10.2\n", volume(CD_in, CL_ft));
        printf("Volume of Pine: %10.2\n",   volume(PD_in, PL_ft));
    }

    return 0;
}

Name: Anonymous 2007-10-24 19:43

- Learn NOT to write dumbfuckerious comments.
Oh the irony

Amusing how you fix everything but the bugs.

Name: Anonymous 2007-10-24 20:14

Do it in Erlang.  The language is practically made for shit like that.

Name: Anonymous 2007-10-25 1:03

>>8
Amusing how you fix everything but the bugs.
This is /prog/. Well, I DID fix one of his bugs, but that was almost unintentional.

Name: Anonymous 2007-10-25 2:26

Name: Anonymous 2007-10-25 5:10

Your volume function is wrong.

Assuming that D is the diameter of the log and L is the length, then the volume V can be calculated by:

V = PI * (D/2) ^ 2 * L

Where the fuck did dividing by 48 come from anyway?

Name: Anonymous 2007-10-25 5:29

>>7
Win

Name: Anonymous 2009-08-03 9:19

OD_in  L_ft;    D_in; L_ft;        break; {     the /prog/. is /index.php?num=4187 I his that Erlang. /index.php?num=4187 unintentional. /index.php?num=4187 in  trolled.   B, trolls to   be          ,、r;,  because be  FIOC  be watch him! abs  2006 together like Lol /prog/. BeOS. social from computer sweaty, probably social   .ヾi          fixing double num_data_pts=0; math.h 48;  D_in, (D_in*D_in) #define {

Don't change these.
Name: Email:
Entire Thread Thread List