Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;printf只出现在最后_C++_C - Fatal编程技术网

C++ C++;printf只出现在最后

C++ C++;printf只出现在最后,c++,c,C++,C,这是我试图学习的程序 程序运行正常,但“键入矩形高度”和“键入矩形宽度”消息仅在程序结束时出现 #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

这是我试图学习的程序

程序运行正常,但“键入矩形高度”和“键入矩形宽度”消息仅在程序结束时出现

#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;
}
#包括
使用名称空间std;
浮点计算器ea(浮点a、浮点b)
{
返回a*b;
}
浮子计算器参数(浮子a、浮子b)
{
返回2*a+2*b;
}
void showMessage(char*msg,float vlr)
{
printf(“%s%5.2f”,msg,vlr);
}
int main()
{
浮子高度、宽度、面积、周长;
printf(“键入矩形高度”);
扫描频率(“%f%*c”和高度);
printf(“键入矩形宽度”);
扫描频率(“%f%*c”和宽度);
面积=计算面积(高度、宽度);
周长=计算周长(高度、宽度);
showMessage(“面积值为=”,面积);
showMessage(“周长值为=”,周长);
返回0;
}

您需要打印换行符:

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

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

当然,您可以打印换行符:

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

或使用C++ IOFSWORES/P>

cout << endl;
cout << flush;

或使用C++ IOFSWORES/P>

cout << endl;
cout << flush;

cout这是因为printf没有立即将字符放到屏幕上。它首先将其复制到缓冲内存中。要在屏幕上实际显示某些内容,应用程序需要将缓冲区与屏幕同步。您可以使用
fflush(stdout)
执行此操作,但也可以在字符串中添加行尾字符(“\n”)。因为标准输出缓冲器在找到“\n”时经常自动同步。更具体的是,Prtf和FFLUH在C++中不仅是正确的功能,而且在C语言中,如果这帮助了你,你能接受这个答案吗?谢谢。但是错误在“输入矩形高度”和“输入矩形宽度”中。我能做同样的事情吗?