1

我正在尝试实现一个隐藏单词查找器游戏,它从文本文件中读取谜题,然后尝试找出隐藏单词的位置。但是,当我尝试进行自上而下的搜索时,屏幕上什么也没有出现,即使我编写了一个独立于该方法的简单 cout 命令。这是代码:(输出什么都没有)

bool WordPuzzle::searchTopToBottom(string word){
  cout << "asdasda";
  string fullWord = "";
  int i = 0;
  int j = 0;
  int index = 0;
  int count;
  bool a = false;
  while (i < numOfColumn){
    while (j < numOfRow){
      if (word[index] == puzzle[i][j]){
        i++;
        index++;
        count++;
        fullWord += word[index];
        if (count == word.size()){
          a = true;
          break;
        }
      }
      else
        j++;
    }
  }
  if (a){
    cout << fullWord;
    return true;
  }
  else{
    cout << "not found";
    return false;
  }
}


int main (){
  cout << "qweqw";
  WordPuzzle w ("puzzle.txt");
  cout << "qweqw";
  w.searchTopToBottom("DEMIR");
  return 0;
}
4

2 回答 2

2

你应该在你endl的末尾添加cout,像这样: cout << variable << endl; 标准输出被缓冲,它会等到你写回车来显示该行。endl添加这个回车。

于 2013-10-14T09:49:47.300 回答
1

要刷新输出缓冲区,只需使用std::flush

std::cout << "my string to be printed" << std::flush;

当你想要换行时,只需写到'\n'行尾:

std::cout << "my string to be printed\n";

或者

std::cout << "my string to be printed" << '\n';

取决于也会刷新输出缓冲区的实现(至少在 linux 上写入终端时)。

一般来说:

  • '\n'当你想要换行时使用,
  • std::flush当您希望刷新输出时使用
  • std::endl当你想要一个换行符并且输出beeing被刷新时使用。
于 2013-10-14T10:20:35.843 回答