簡體   English   中英

C跳過scanf的編程輸入錯誤

[英]C Programming input error with scanf skipping

我正在介紹C編程課程,我有點困惑。 我的輸入無法正常工作。 當我運行該程序時,該程序將接受第一個輸入,然后跳過其余的輸入提示,並生成總計。

到底是怎么回事? 我完全迷路了。

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 */

並且您不能指定精度,而是可以指定最大字符數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM