Thread: New to this type of Programming, please help?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    New to this type of Programming, please help?

    I just started a new programming class last week, using the free Microsoft Visual C++. This is all new to me, so I apologize in advance if this seems like a very minor problem. Anywho, the program we're required to do this week is one where the user can enter in a radius, and the program will compute the surface area and volume of a sphere. I'm required to use doubles though, instead of floats, and our teacher said to use the #define function to define pi.

    This is what I've come up with so far, and it doesn't seem to be working. I've been tweaking things here and there, but it doesn't seem to work. Can anybody point out where I've gone wrong? Thank you!

    Code:
    #include <stdio.h>
     
    int main () {
           double r;
           double surface_area_sphere, volume_sphere;
     
    #define pi 3.141593;
           printf("Enter radius of sphere (cm): ");
           scanf("%lf",&r);
          
           printf("Radius: %lf\n",&r);
     
           surface_area_sphere = 4.0*pi*r*r;
           printf("Surface Area: %lf\n", surface_area_sphere);
     
           volume_sphere = (4.0/3)*pi*r*r*r;
           printf("Volume: %lf\n", volume_sphere);
     
           return 0;
    }
    
    

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is what I've come up with so far, and it doesn't seem to be working.
    You'll need to be more specific. Does it compile and run, or does it give you errors? If it does not compile and run, then please provide the errors you are receiving. If it runs, but the output is not what you expect, then please provide the input you're giving and the output you expect.

    By the way, this is C code, not C++, and in the future should be posted to the C Programming forum rather than C++ Programming. I'll report the thread for moving.

    Ah, I see. Here:

    Code:
    printf("Radius: %lf\n",&r);
    You don't use & on a printf, otherwise you're printing the address in memory where the value is stored, rather than the value.
    Last edited by rags_to_riches; 09-15-2012 at 09:00 AM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    but it doesn't seem to work
    I left my pocket Oracle at home, please read the FAQ to state properly your problem next time.

    A quick test shows two minor issues: 1/ [see previous post], 2/ preprocessing directives **are not C statements**, so remove the trailing semi-colon.
    Last edited by root4; 09-15-2012 at 09:02 AM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    This is the error it gives me:

    Code:
    1>------ Build started: Project: Program 2, Configuration: Debug Win32 ------
    
    1>  prog2_io.c
    
    1>prog2_io.c(9): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    
    1>stdio.h(304) : see declaration of 'scanf'
    
    1>prog2_io.c(13): error C2100: illegal indirection
    
    1>prog2_io.c(13): warning C4552: '*' : operator has no effect; expected operator with side-effect
    
    1>prog2_io.c(16): error C2100: illegal indirection
    
    1>prog2_io.c(16): warning C4552: '*' : operator has no effect; expected operator with side-effect
    
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Also, after the program runs, the first printf line will pop up prompting the user to enter a radius, and then when you click enter it spits out a Volume, but I don't think it's computing correctly. For example, if you enter in a radius of 1, it'll give you a volume of 3.141593.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I am surprised this compiled.

    Code:
    #define pi 3.141593;
    You need to remove the ; at the end.

    My results:

    Code:
    Radius: 1.000000
    Surface Area: 12.566372
    Volume: 4.188791

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Thanks so much! It's working fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a list pointer of parent type from base type...
    By Adamkromm in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2012, 02:07 PM
  2. C programming - incompatible argument type
    By COG92 in forum C Programming
    Replies: 23
    Last Post: 10-17-2011, 10:48 PM
  3. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  4. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  5. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM