help with scanf...

This is a discussion on help with scanf... within the C Programming forums, part of the General Programming Boards category; I use printf & scanf in the program below. I enter a value and then the value is used in ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    help with scanf...

    I use printf & scanf in the program below.
    I enter a value and then the value is used in the formulas.

    But all the program does is printing
    enter length :
    and although I can put some number... the program does nothing.
    No error, no warning, nothing...
    It just stops there.

    So I think there's something wrong aobut the scanf...
    Can someone help me?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define PI 3.14
    #define g 9.807
    
    int main(void)
    {
        double period, length; 
            
        printf("enter length : ");
        scanf("%lf\n", &length);
            
        period=2*PI*sqrt(length/g);
        length=(period*period*g)/(4*PI*PI);
        
        printf("period is %g\n", period);
        printf("length is %g\n", length);        
        
        getch();
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    I have executed your program.It worked correctly only.

    I have received the following as a output of your program.

    Output
    --------
    enter length : 2
    period is 2.836
    length is 2

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    What? It won't work in mine.
    Still stops working after I enter a value behind : enter the length

    Is it compiler problem?
    I'm using devcpp but my files are .c

    I changed my compiler to VisualC++ but it still won't work...
    Last edited by vsovereign; 03-01-2010 at 12:56 AM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: help with scanf...

    For me it is asking two inputs because you have used newline in the scanf format.

    If you removed that it will ask only one time.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define PI 3.14
    #define g 9.807
    
    int main(void)
    {
        double period, length;
    
        printf("enter length : ");
      scanf("%lf", &length);
        fflush(stdin);
    
        period=2*PI*sqrt(length/g);
        length=(period*period*g)/(4*PI*PI);
    
        printf("period is %g\n", period);
        printf("length is %g\n", length);
    
        return 0;
        }

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Can u tell me by which way have you compiled your code?
    And can you ensure that you have compiled with -lm option?
    Last edited by thillai_selvan; 03-01-2010 at 01:10 AM.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by thillai_selvan View Post
    Can u tell me By which way have you compiled your code?
    And can you ensure that you have compiled with -lm option?
    I'm sorry I'm a newbie.
    I don't understand your question.

    I compile my program...well I use a compiler...I don't know which way the compiler does the compiling.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by sganesh View Post
    For me it is asking two inputs because you have used newline in the scanf format.

    If you removed that it will ask only one time.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define PI 3.14
    #define g 9.807
    
    int main(void)
    {
        double period, length;
    
        printf("enter length : ");
      scanf("%lf", &length);
        fflush(stdin);
    
        period=2*PI*sqrt(length/g);
        length=(period*period*g)/(4*PI*PI);
    
        printf("period is %g\n", period);
        printf("length is %g\n", length);
    
        return 0;
        }
    You're right!! I removed the \n and the program works! THANKS!

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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