0

so i made this on repl.it :

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;

int main(){
    for(int i=0; i<100; i++){
        for(int j=0; j<30; j++){
            for(int k=0; k<40; k++){
              cout<<"■"<<' ';
            }
            cout<<endl;
        }
        this_thread::sleep_for(chrono::seconds(1));
        system("clear");
    }
}

Issue

When i run the code, occasionally, exactly 2 characters right next to each other are replaced with the character on mostly the lower half of the output.

Tried:

Replacing the single quote for the space character after the square character with double quote, moving the space character into the square character string:

Result: no observable changes.

Removing the space character:

Result: the issue still exists, but it occurs less frequently.

Reducing the amount j and k iterates:

Result: apparently the characters no longer gets printed, but I doubt if the problem has really been solved (the probability of the issue occurring could have just been lowered to the point that I can't consistently reproduce the problem).

oilpeanut
  • 1
  • 1
  • 1
  • 2
    Did not reproduce on my machine. Could be a bug with your terminal, especially if the UTF-8 sequence of the block character is being interrupted with other characters (such as control characters for terminal I/O). – Eljay Nov 23 '20 at 03:25
  • 2
    According to [this](https://en.cppreference.com/w/cpp/utility/program/system) system will perform this function through a different thread using `popen`, is there a chance that this is not thread safe? – Fantastic Mr Fox Nov 23 '20 at 03:29
  • 2
    Although it says *"An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O"* which you do ... – Fantastic Mr Fox Nov 23 '20 at 03:30
  • You should probably look into ncurses and adjacent libraries if you want to draw something. Calling `clear` several times per second will flicker your screen badly. – Botje Nov 23 '20 at 08:53
  • Instead of calling `clear` you can do the job yourself by printing a terminal sequence. That will clear up any buffering issues. Make sure to flush! [Clearing terminal in Linux with C++ code](https://stackoverflow.com/questions/4062045/clearing-terminal-in-linux-with-c-code) – Botje Nov 23 '20 at 08:54
  • @Botje >Make sure to flush! But where should I flush, and do I use `cout.flush();` or do I append `< – oilpeanut Nov 23 '20 at 14:06
  • You should flush (both methods are fine) right after printing the escape sequence I linked to. – Botje Nov 23 '20 at 14:08
  • @Botje unfortunately it did not work for me, the result remained the same as the original code. – oilpeanut Nov 23 '20 at 14:24
  • Are you still calling `clear` or are you printing the escape sequence for clearing? – Botje Nov 23 '20 at 14:25
  • @Botje i replaced the `system("clear");` with `cout<<"\033[2J\033[1;1H"< – oilpeanut Nov 23 '20 at 14:27
  • Out of ideas then, sorry. – Botje Nov 23 '20 at 14:28

0 Answers0