在C ++上使用fflush [英] using fflush on C++

查看:202
本文介绍了在C ++上使用fflush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我在C ++中使用fflush

Can someone help me using fflush in C++

这是C语言中的示例代码

Here is a sample code in C

#include <stdio.h>
using namespace std;

int a,b,i;
char result[20];

int main() {
  scanf("%d %d\n", &a, &b);
  for (i=1; i<=10; i++) {
    printf("5\n");
    fflush(stdout);
    gets(result);
    if (strcmp(result, "congratulation") == 0) break;
  }
  return 0;
}

这是用于获取交互式输入的程序.

This is program for getting interactive input.

我通常使用cincout,所以有可能不使用printfscanf吗?

I usually use cin and cout so is it possible not using printf and scanf?

推荐答案

对C ++编程 style 的翻译是:

The translation to C++ programming style is this:

#include <iostream>

using std::cin;
using std::cout;
using std::string;

int main() {
  string line;
  int a, b;

  if (cin >> a >> b) {
    for (int i = 0; i < 10; i++) {
      cout << "5" << std::endl; // endl does the flushing
      if (std::getline(cin, line)) {
        if (line == "congratulations") {
          break;
        }
      }
    }
  }
  return 0;
}

请注意,我故意添加了一些错误检查.

Note that I deliberately added some error checking.

这篇关于在C ++上使用fflush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆