-2

I'm attempting to create a program that computes a value Vt given values for Vo, a, and tm. When I run the code, the program asks the first question then after I give it a value, it speeds through the code without letting me give values for the other two variables. I am programming in C. This is the code:

#include <stdio.h>

main () {

    float Vt;
    float Vo;
    float a;
    float tm;

    printf(" At what time in flight do you wish to know the velocity? To the 
    nearest hundredth. :");
    scanf(" %.2f", &tm);

    printf (" What is the angle of trajectory? :");
    scanf (" %.2f", &a);

    printf (" What is the initial velocity? :");
    scanf (" %.2f", &Vo);

    float sina = sin (a);
    float cosa = cos (a);
    float tana = tan (a);

    Vt = sqrt((pow((Vo * cosa), 2.0)) + (pow((Vo * sina - (9.8 *tm)), 2.0)));

    printf("\n\n\n\n %.3f", Vt);

    return 0;

}

1 Answers1

3

Your scanf format strings are invalid. Change them to "%f". That is, change

  • scanf(" %.2f", &tm); to scanf("%f", &tm);
  • scanf(" %.2f", &a); to scanf("%f", &a);
  • scanf(" %.2f", &Vo); to scanf("%f", &Vo);

If you intended to scan at most N characters then you must use the "%Nf" format string (such as "%2f" or "%3f"), where N is an integer larger than 0.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
MarkWeston
  • 740
  • 4
  • 15