c - 我该如何修复我的代码?我认为问题是第一个scanf?

标签 c

我正在尝试创建一个程序,根据给定的 Vo、a 和 tm 值计算 Vt 值。当我运行代码时,程序会询问第一个问题,然后在我给它一个值后,它会加快代码的运行速度,而不会让我为其他两个变量提供值。我正在用 C 编程。这是代码:

#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;

}

最佳答案

您的 scanf 格式字符串无效。将它们更改为“%f”。即改变

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

如果您打算扫描最多 N 个字符,则必须使用"%Nf" 格式字符串(例如"%2f""%3f"),其中 N 是大于 0 的整数。

关于c - 我该如何修复我的代码?我认为问题是第一个scanf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376124/

相关文章:

c - 在 c 文件中执行 bash 命令并将输出存储在数组中

c++ - Windows 临界区公平性

c - 如何将对话框位置坐标复制到另一个对话框中?

c - Intel 指令的 LOCK 前缀。重点是什么?

c - 如何在 C/C++ 中对具有多个相似值的枚举进行 switch()?

c - 使用 scanf() 读取不超过字符串的大小

c - 函数调用后指针返回值改变

c - 使用指针从多维数组中获取值 - C

c++ - 宏引用扩展结果

objective-c - 如何更改进程名称 objective-c