C 编程输入错误,scanf 跳过

标签 c

我正在学习 C 编程入门类(class),我有点困惑。 我的输入无法正常工作。当我运行程序时,程序将接受第一个输入,然后跳过其余的输入提示并生成总计。

这是怎么回事?我完全迷失了。

int main(void) {
  /* declarations: */
  double y, m1, s1, s2, m2, i;
  char g; /* Do not remove this line */

  /*Print the title of the program*/
  printf("       MILEAGE SPEED INTERPOLATION\n");
  printf("______________________________________________________________________\n");

  printf("Enter in your first speed:\n"); //this is s1
  scanf("%.1f", &s1);

  printf("Enter in your mileage:\n"); //m will be mileage 1
  scanf("%.1f", &m1); //mileage is y

  printf("Enter in your second speed:\n"); //this is s2
  scanf("%.1f", &s2);

  printf("Enter in your second mileage:\n");
  scanf("%.1f", &m2);

  /*Get the needed input*/
  /*get the interpolation from the user*/
  printf("Enter your interpolation:\n"); //number between 1 and 2
  scanf("%.lf", &i);
  printf("______________________________________________________________________\n");

  /*Statements that perform the desired task*/ 
  // This equation is calculating from rpm to knots
  y = m1 + (i - s1) * (m2 - m1) / (s2 - s1); //This will do the equation for us. 
  /*  Printing of the results*/

  // Trying to print the   answer from above equation
  printf("Your estimated value is %f: ", y); 

  /*Keeps the window open.  Please do not alter this.*/
  printf("\n\nEnter to q to quit.\n");
  do {
    g = getchar();
  } while (g != 'q');

  return 0;
}

最佳答案

您必须将 "%lf" 用于 doublescanf()

if (scanf("%lf", &s1) != 1)
    return -1; /* Bad input */

并且您无法指定精度,但可以指定最大字符数。

关于C 编程输入错误,scanf 跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32729072/

相关文章:

c - 估计erlang数据结构的内存范围

c - 如何迭代节点列表并比较字符串

在 C 中从二进制 "components"构造一个 int

c - 关于 sigsetjmp 和 siglongjmp 的问题

c++ - 如何将其他 .wo、.so、.a 文件链接到当前 cuda 编译

c - `typedef struct foo {int bar};`的合法性

c - 使用 usart 重新定义 stdio 的 Printf 无法正确格式化

c - 动态内存指针

c - 将 X509_SIG 与早期的 openssl 一起使用

c - Mac OSX 上指针的最大值是多少?