c++ - 构造函数中的打印函数

标签 c++ constructor

下面是我的代码的一部分(使用构造函数)。在这里,当我运行代码时,scanf 之前的 printf 没有出现。我得到一个空白屏幕,我必须在其中输入否,然后显示 printf 结果。有人可以帮我吗?我需要显示“输入点数”,以便我可以了解我正在输入的条目

Discret::Discret()
{

  rich = 0;
  solve  = 0;
  total  = 0;
  int no;
  double no;
  printf("\n\n");
  printf("==============================================================\n");
  printf("Input:\n");
  printf("Enter the number of points" );
  scanf("%d",&no);
  printf("==============================================================\n");

  /************************************
   *
   * Material
   *
   ************************************/
  double youngs, poisson;

  S  = 1000;
  P = 0.0;

  material.resize(1);
  material[0] = new Material(youngs, poisson);

}

最佳答案

printf 适用于缓冲流。你需要冲洗它

printf("Enter the number of points" );
fflush( stdout);
//...
scanf("%d",&no);
printf("==============================================================\n");
fflush( stdout);

在使用 C++ 流时,您将使用

std::cout.flush();  // or std::cout << flush;

关于c++ - 构造函数中的打印函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874316/

相关文章:

c++ - 适用于 (auto& 一个 : a) grammatically right?

c++ - Client Server场景下复杂且相互关联的数据结构

c++ - 取消映射缓冲区 glBindBuffer() 时出现段错误

c++ - 使用条件变量在 Linux 中实现 Windows 事件?

C++ 可变参数构造函数 - 传递给父构造函数

c++ - 回到 C++,带指针的 void 函数,如何制作 get 函数

c++ - 数组作为函数参数 - 编译错误

syntax - F# 构造函数语法 - 覆盖和扩充 new

parameters - MEF+MVVM : How to instantiate a class with non-importing or mixed parameters

派生类的 C++ 构造函数,其中基类包含类成员