c++ printf只出现在末尾
C++ printf only appear in the end
这是我试图学习的程序
程序可以工作,但是"输入矩形高度"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
是行缓冲的—这意味着您写入流的内容将被缓冲,直到写入换行字符。此时,缓冲区将被刷新并实际写入控制台。
相关文章:
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 内联程序集printf将整数解释为地址
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- C++ Setter/Getter,cout 工作,printf 失败
- Printf 命令不打印时添加了查找常见除数的新代码
- SIGSEGV, 分段错误. 而 printf() 数组索引的值
- 为什么在包含iostream时可以使用printf()?
- 创建具有验证和语法突出显示的自定义 printf
- 0x%08lx 格式说明符在 printf 语句到 cout
- C printf 无法从终端按预期工作
- Printf 和 Echo 提供不同的输出
- C++ - scanf() 和 printf() 执行顺序不对
- cout 打印不准确的结果,printf 打印准确的结果
- 了解 printf 格式规范中的 %03.3u
- redefine printf(), sprintf(), etc. arm-none-eabi toolchain
- 为什么 printf 在 C++ 中的执行速度比 cout 快?另外scanf比cin慢,为什么?
- C++, printf vs cout performance
- printf 数据类型说明符复杂问题
- 在"printf"中使用标签"h"或"hh"是否涉及未定义的
- printf() 中 %f 和 %F 之间的区别?