printf没有给我正确的输出,也许是scanf的原因,请咨询

编写代码以接受用户输入(例如P0 x1 y1)存储在嵌套结构中作为一个点,其他两个点则相同。但是,当用scanf输入并检查printf时,我得不到正确的数字,就像scanf正在读取其他内容一样,建议会很棒,谢谢!

float x, x2, y, y2;
char Q, input;

nested triangle;

scanf("%c", &input);

if (input == 'Q' || input =='q')
  return;

else
  {
  scanf(" %c %f%f",&input, &triangle.P0.x, &triangle.P0.y);
  printf("points are\n%f \n%f \n", triangle.P0.x, triangle.P0.y);
}

scanf("%c", &input);

if (input == 'Q' || input =='q')
  return;

else
  {
  scanf(" %c %f%f",&input, &triangle.P1.x, &triangle.P1.y);
  printf("points are\n %f \n%f \n",input, triangle.P1.x, triangle.P1.y);
}
评论
  • 伪淑女
    伪淑女 回复

    If you write scanf("%c", &input);, then this will read in a new line from a previous input into input. This is usually not what you intend.

    Therefore, write scanf(" %c", &input); (note the blank before the %c) to skip white spaces.

    Note further that %c will just read in one character; if you intend to read in P0, you need a string, e.g. char[3] and format specifier %s.