Thread: where is the problem?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    where is the problem?

    after i run the program, when i enter an integer number it asks me to calculate V for velocity or T for time but when i enter v or t id does not accept it




    Write a complete C program that performs gravity calculations based on Isaac Newton's law of universal gravitation. Your program must do this:



    1. How long (in seconds – 2 decimal places) does it take an object to fall distance d? (d, in meters, being entered by the user at the keyboard).
    2. What is the velocity (in meters per second – 2 decimal places) of an object that has traveled d meters?
    3. Ask the user for the desired computation (1) for time, (2) for velocity and (3) to end program.
    4. Your program must return to the choices until the user enters option (3).


    Code:
    
    
    int
    main()
    
    {
    char variable;
    int d= 0.0;
    float sqvelocity =0.0;
    float sqtime =0.0;
    
    printf("Enter a value for distance:");
    scanf("%d",&d);
    
    printf("Enter variable for desired computation, v (velocity) or t (time):");
    scanf("%c", &variable);
    
    if (variable == 'v')
    printf("the velocity is: %lf.\n", sqvelocity);
    else
    if (variable == 't')
    printf("the time is: %lf.\n", sqtime);
    
    sqvelocity= sqrt(2*9.8*d);
    sqtime= sqrt((2*d)/9.8);
    
    return (0);
    }
    Hello admins would you delete my post.
    NO!

    Thanks
    Last edited by Salem; 10-17-2012 at 12:28 AM. Reason: restored original code

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    After you type in the distance, you hit enter. That enter puts a new line ('\n') character in the input buffer. That '\n' is not a number, so %d doesn't read it, it leaves it there. Then when you call scanf("%c", &variable), the %c reads the next character, the new line, not the 'v' or 't' you type. Read this post (soon to be a FAQ article here) for more info and possible solutions: http://cboard.cprogramming.com/c-pro...ml#post1126782.

    The quick fix is to put a space before the %c:
    Code:
    scanf(" %c", &variable);
    The space skips any whitespace (space, tab, vertical tab, newline).

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your main problem is that, when you enter the distance, you type a number and press enter - there are now two objects in the input stream - the integer, and the newline character. For instance,

    Code:
    4\n
    The newline remains in the input buffer, and is picked up by the next "scanf()", giving the appearance that the second "scanf()" was skipped.

    One way around it is to tell the second "scanf()" to skip any leading whitespace - this can be done by placing a space before the format specifier:

    Code:
    scanf(" %c",&variables);
    Some other observations:

    - If 'd' is an integer, just assign it 0 (and not 0.0)
    - You're printing out "sqvelocity" and "sqtime" before they are calculated

    [EDIT] Too slow again!

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    Thanks

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Hello admins would you delete my post.
    You shouldn't have deleted your code in #1. Your problem may have helped someone else.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Fixed
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM

Tags for this Thread