c++ - Can't finish the do-while loop by pressing enter key -


i want finish do-while loop when pressed enter key. have idea solve problem? tried check ascii code enter key didn't work out.

                  do{                     for(int dongu=0;dongu<secimsayi;dongu++)                     {                         cout<<"-";                         sleep(1);                         if(dongu==secimsayi-1)                         {                             cout<<">"<<endl;                         }                     }                     for(int dongu2=0;dongu2<secimsayi;dongu2++)                     {                         cout<<" ";                     }                      for(int dongu3=secimsayi;0<dongu3;dongu3--)                     {                          cout<<"-\b\b";                         sleep(1);                                                        if(dongu3==1)                         {                             cout<<"<"<<endl;                          }                      }                  }while(getchar()== '\n');  //i want end do-while loop when pressed enter 

i don't believe there standard solution in c++ , not way standard console programs work.

the standard way using threads, 1 thread waits until line read , signals main thread setting std::atomic<bool>.
(see example)

another solution right library according operating system. on linux, use ncurses, on windows, there support. should give more control on output of program.

example threaded approach:

#include <iostream> #include <thread> #include <chrono> #include <atomic>  class waitforenter { public:   waitforenter() : finish(false)   {     thr = std::thread([this]() {          std::string in;          std::getline(std::cin, in);         finish = true;        });   }   ~waitforenter()   {     thr.join();   }   bool isfinished() const { return finish; } private:   std::atomic<bool> finish;   std::thread thr; };  int main() {     waitforenter wait;     while (! wait.isfinished())     {       std::cout << "." << std::flush;       std::this_thread::sleep_for(std::chrono::seconds(1));     }     std::cout << "\nfinished\n"; } 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -