programming help, if this is allowed

Discussion in 'OT Technology' started by CXHatchback, Jan 30, 2003.

  1. CXHatchback

    CXHatchback Guest

    I'm trying to do my C homework, but can't figure out what's wrong with my code so far and I don't understand the error. Any help would be appreciated.

    Here's my code:

    // problem 3

    #include <stdio.h>

    int main( void ) {

    int radius,length,volFeet,volInches;

    printf("Welcome to the Cylinder Information Calculator\n\n");
    printf("Please enter the radius of the clyinder in feet\n");
    scanf("%d",&radius);
    printf("Now enter the length of the cylinder in feet.\n");
    scanf("%d",&length);

    volFeet=3.14*radius^2*length
    volInches=volfeet*1728

    printf("The Volume of the cylinder in cubic feet is: ");
    printf("%d\n\n",&volFeet);

    printf("The volume of the cylinder in cubic inches is: ");
    printf("&d\n\n",&volInches);


    return 0;
    }


    When I try to compile the program, I keep getting errors saying "illegal operand" and it underlines volInches in the 10th line. I've checked all kinds of things, and changed the variable names and added/removed ";" and I still can't get it to run. Most of the time even printf is an illegal operand and I don't know what's wrong. This is my first semester in C, but I've done basic before so I have a little programming background, but I'm still at a loss.
     
  2. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    Don't declare volFeet,volInches as ints. You'll lose precision when you perform the calculation. Use floats.

    You need a semicolon on these lines:
    volFeet=3.14*radius^2*length
    volInches=volfeet*1728

    volfeet should be volFeet in that last line too.

    radius^2 is also not valid syntax. Just do radius*radius

    In printf("&d\n\n",&volInches); you should put %d, not &d

    Those are just off the top of my head. I don't have a C compiler handy to check.
     
  3. CXHatchback

    CXHatchback Guest

    Oh man thanks so much for a couple of those I didn't see. I think it was the radius^2 that fixed the problem for the most part.

    And yeah I was going to use float, but if I can get away with it I think I'll use int anyway.
     
  4. CXHatchback

    CXHatchback Guest

    Okay I've fixed it to this:

    // problem 3

    #include <stdio.h>
    #define PI 3.141
    int main( void ) {

    int radius,length;
    float volFeet,volInches;

    printf("Welcome to the Cylinder Information Calculator\n\n");
    printf("Please enter the radius of the clyinder in feet\n");
    scanf("%d",&radius);
    printf("Now enter the length of the cylinder in feet.\n");
    scanf("%d",&length);

    volFeet=PI*radius*radius*length;
    printf("The Volume of the cylinder in cubic feet is: ");
    printf("%d\n\n",&volFeet);

    volInches=volFeet*1728;
    printf("The volume of the cylinder in cubic inches is: ");
    printf("%d\n\n",&volInches);


    return 0;
    }


    The program runs, but I'm getting 1245036 as an answer when I put it 5 for the radius and 10 for the length. Is there something that I'm missing here or don't know about arithmetic in C?
     
  5. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    It should be %f... And remove the ampersands:

    printf("%f\n\n",volFeet);
    printf("%f\n\n",volInches);
     
  6. 6Double5321

    6Double5321 Into the blue again, after the money's gone

    Joined:
    Nov 6, 2001
    Messages:
    7,356
    Likes Received:
    0
    Location:
    cartersville
    I don't know jack about C, so I can't help there, but going over the math:

    volFeet = 3.141 * 5 * 5 * 10 = 785.25

    volInches = volFeet * 1728 = 1356912

    Not sure if this helps, but I don't know C yet...just figured I'd verify the numbers.
     

Share This Page