编写代码以接受用户输入(例如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 intoinput
. 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 inP0
, you need a string, e.g.char[3]
and format specifier%s
.