c++ printf只出现在末尾

C++ printf only appear in the end

本文关键字:printf c++      更新时间:2023-10-16

这是我试图学习的程序

程序可以工作,但是"输入矩形高度"answers"输入矩形宽度"只有在程序超过

时才会出现。
#include < stdio.h >
using namespace std;
float calculateArea(float a, float b)
{
    return a * b;
}
float calculatePerimeter(float a, float b)
{
    return 2*a + 2*b;
}
void showMessage(char *msg, float vlr)
{
    printf("%s %5.2f", msg, vlr);
}
int main()
{
    float height, width, area, perimeter;
    printf("type the rectangle height");
    scanf("%f%*c", &height);
    printf("type the rectangle width");
    scanf("%f%*c", &width);
    area = calculateArea(height, width);
    perimeter = calculatePerimeter(height, width);
    showMessage("The area value is =", area);
    showMessage("The perimeter value is =", perimeter);
    return 0;
}

当然你可以打印换行符:

printf("n");

或使用c++的iostreams

cout << endl;

如果你愿意,你可以强制程序刷新它的输出流:

fflush(stdout);

或使用c++的iostreams

cout << flush;

如果您不希望输入换行符,则不必输入换行符。

你需要打印一个换行符:

void showMessage(char *msg, float vlr)
{
    printf("%s %5.2fn", msg, vlr);
    //          ----^
}

原因是,默认情况下,stdout行缓冲的—这意味着您写入流的内容将被缓冲,直到写入换行字符。此时,缓冲区将被刷新并实际写入控制台。