What is wrong with my code?

This is a discussion on What is wrong with my code? within the C Programming forums, part of the General Programming Boards category; Can anyone tell me why my C code can't compile: Code: #include <stdio.h> #include <math.h> #define PI = 3.1415926 int ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    What is wrong with my code?

    Can anyone tell me why my C code can't compile:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI = 3.1415926
    
    int main()
    {
    int k;
    double R, A, V, TR, TA, TV, R_av, A_av, V_av;
    for (k = 1; k <= 15; k++) {
    R=k/10;
    A=4*PI*R*R;
    V=A*R/3;
    TA=TA+A;
    TV=TV+V;
    TR=TR+R;
    printf ("Sphere ( %d)\t Radius =  %d\t Area =  %d\t Volume =  %d\n", k, R, A, V)
    ;
    R += 0.1;
    }
    
    
    }
    This is the error it displays:

    iacs5.ucsd.edu% cc hw2v2.c -o hw2
    "hw2v2.c", line 11: whitespace between two character assignment operators
    "hw2v2.c", line 11: left operand must be modifiable lvalue: op "*="
    cc: acomp failed for hw2v2.c

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    You accidentally put an '=' in your #define PI. Remember, #define's are textual preprocessing substitutions. EVERYTHING to the right of PI will be substituted in for every occurrence of PI in your code.

    So this is what the substitution will look like:
    Code:
    Before:
    A=4*PI*R*R;
    
    After:
    A=4*= 3.1415926*R*R;

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Thanks for the tip arpsmack!

    Now I can compile my code but my output is like this:

    Sphere ( 1) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 2) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 3) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 4) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 5) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 6) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 7) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 8) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 9) Radius = 0.0 Area = 0.000 Volume = 0.000
    Sphere ( 10) Radius = 1.0 Area = 12.566 Volume = 4.189
    Sphere ( 11) Radius = 1.0 Area = 12.566 Volume = 4.189
    Sphere ( 12) Radius = 1.0 Area = 12.566 Volume = 4.189
    Sphere ( 13) Radius = 1.0 Area = 12.566 Volume = 4.189
    Sphere ( 14) Radius = 1.0 Area = 12.566 Volume = 4.189
    Sphere ( 15) Radius = 1.0 Area = 12.566 Volume = 4.189


    This is the code:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.1415926
    
    int main()
    {
    int k;
    double R, A, V, TR, TA, TV, R_av, A_av, V_av;
    for (k=1;k<=15;k++) {
    R=k/10;
    A=4*PI*R*R;
    V=A*R/3;
    
    printf ("Sphere ( %d) Radius =  %4.1f Area =  %7.3f Volume =  %7.3f\n",k,R,A,V);
    }
    
    
    }
    Why are the radius, area, and volume equations not calculating properly? Thanks again.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > R=k/10;
    k is also integer, so you get integer division, which truncates.
    Try
    R=k/10.0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Code:
    R=k/10;
    On the right hand side, 'k' and 10 are both integers, so this is going to result in integer division. Which means, if 'k' is less than 10, R is going to be set to 0 (integer division truncates everything after the decimal point).

    Changing the 10 to a 10.0 will make it a double instead of an int.

    [edit] heh [/edit]

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by arpsmack View Post
    Code:
    R=k/10;
    On the right hand side, 'k' and 10 are both integers, so this is going to result in integer division. Which means, if 'k' is less than 10, R is going to be set to 0 (integer division truncates everything after the decimal point).

    Changing the 10 to a 10.0 will make it a double instead of an int.

    [edit] heh [/edit]
    You have to wake up pretty early in the morning to beat ol' Salem. Or for that matter, you have to be drink a LOT of caffein to beat me and tabstop.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    One thing else you could do: improve your indentation. It's a mess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21