Super Beginner Question

This is a discussion on Super Beginner Question within the C Programming forums, part of the General Programming Boards category; I am just working on my very first C program ever for a programming class. The assignment is very simple. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Super Beginner Question

    I am just working on my very first C program ever for a programming class. The assignment is very simple. Enter a program that takes the radius of a sphere and finds the surface area and volume of it. My program compiles, but when it runs it doesn't run properly and I was wondering if someone could look over it and help me figure out whats wrong with it.

    Heres the code:

    Code:
    /* Sphere.c */
    #include<stdio.h>
    #include<stdlib.h>
    int main(void){
       double r, v, s;
       const double pi = 3.141592654;
    
       printf("Enter the radius of the sphere: ");
       r = scanf(" %lf ", &r);
       v = 4.0/3.0*pi*r*r*r;
       s = 4*pi*r*r;
       printf("The volume is %lf and the surface area is %lf.", v, s);
    
       return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Stop assigning scanf's return value to r. You pass the objects that you want *scanf functions to assign as pointers, and if it works, the function takes care of the assignment for you. All the *scanf functions return the number of objects assigned, which means that you are overwriting the user's input at every run.

    Might as well read this.

    It is good that you are paying attention to scanf's return value, however. It's the only way to discern if the function worked correctly.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    Ok I fixed it, but now when I run it i get this error:
    Segmentation Fault

    heres the new code.

    Code:
    /* Sphere.c */
    #include<stdio.h>
    #include<stdlib.h>
    int main(void){
       double r, v, s;
       const double pi = 3.141592654;
    
       printf("Enter the radius of the sphere: ");
       r = scanf(" %lf" );
       v = 4.0/3.0*pi*r*r*r;
       s = 4*pi*r*r;
       printf("The volume is %lf and the surface area is %lf.", v, s);
    
       return 0;
    }

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're not passing any variables to scanf(). Your previous code was better in that respect.

    But what you want is probably this:
    Code:
    scanf(" &#37;lf", &r);
    Also, printf() uses %f for floats and doubles, while scanf() uses %f for floats and %lf for doubles.

    I would also suggest using better variable names, names longer than one character.

    BTW, you don't use anything from stdlib.h anywhere.

    [edit]
    It is good that you are paying attention to scanf's return value, however. It's the only way to discern if the function worked correctly.
    But it is bad that scanf()'s return value is being assigned to a double, the same variable that the OP wanted to store scanf()'s value into. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Don't miss the point now.
    Code:
    /* Sphere.c */
    #include<stdio.h>
    
    int main(void){
       double r, v, s;
       const double pi = 3.141592654;
       int check = 0;
       
       printf("Enter the radius of the sphere: ");
       check = scanf("%lf", &r ); 
       /* Store whatever the user typed at the address of r. */ 
       if( check == 1 ) {
         /* Recall that scanf returns the number of objects assigned, 
            like the rest of the *scanf family. */
         v = 4.0/3.0*pi*r*r*r;
         s = 4*pi*r*r;
         printf("The volume is %lf and the surface area is %lf.", v, s);
       }
       
       return 0;
    }

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by dwks View Post
    [edit]
    But it is bad that scanf()'s return value is being assigned to a double, the same variable that the OP wanted to store scanf()'s value into. [/edit]
    That's what I said. People have really struggled to pay attention to what I write lately. I appologize for this. There must be something wrong with the way that I write.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another super easy question
    By joker_tony in forum C Programming
    Replies: 5
    Last Post: 03-29-2008, 01:41 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. A very beginner question!
    By sifeet in forum C Programming
    Replies: 8
    Last Post: 11-06-2007, 07:13 AM
  4. super noob question
    By verbity in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2007, 12:38 PM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM

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