简体   繁体   中英

C Programming input error with scanf skipping

I am in a intro to C programming course and I am a bit confused. My input is not working correctly. When I run the program, the program will take the first input, then skip the rest of the prompts for inputs and generate a total.

What is going on? I am completely lost.

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

You must use "%lf" for double with scanf()

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

and you can't specify precision, instead you can specify a maximum number of characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM