user2604150
user2604150

Reputation: 409

C++ cout does not work with the method

I'm trying to implement a hidden word finder game, it reads the puzzle from the text file and then tries to find out where the hidden word is. However, when I try to make a top top bottom search, nothing appears on the screen, even when I write a simple cout command independent from the method. Here is the code: (Output is nothing btw)

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;
}

Upvotes: 1

Views: 3650

Answers (2)

villekulla
villekulla

Reputation: 1085

To flush the output buffer, just use std::flush:

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

When you want a newline, just write '\n'to the end of a line:

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

or

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

Depending on the implementation that also would flush the output buffer (at least on linux when writing to a terminal).

Generally:

  • use '\n' when you want a newline,
  • use std::flush when you want the output to be flushed
  • use std::endl when you want a newline and the output beeing flushed.

Upvotes: 1

Co_42
Co_42

Reputation: 1269

You should add endl at the end of your cout, like that : cout << variable << endl; The standard output is buffered and it will wait until you wrote a carriage return to display the line. endl add this carriage return.

Upvotes: 2

Related Questions