1

A C++ program that perform such:

 cout<<"Hello please enter in your age: "; 
 int age;
 cin>>age; 

While the system waits for input, I want to display this following loop:

for(;;)
{
 cout << "Waiting.............." << '\r' << flush; Sleep(500);
 cout << ".......Waiting......." << '\r' << flush; Sleep(500);
 cout << "..............Waiting" << '\r' << flush; Sleep(500);
}

The loop should stop when there is any input.

dlavila
  • 1,204
  • 11
  • 25
user3651085
  • 61
  • 3
  • 7
  • i found this http://stackoverflow.com/questions/28790570/performing-infinite-loop-while-awaiting-input But i need it in c++ please – user3651085 May 08 '15 at 13:25
  • 1
    http://stackoverflow.com/questions/21257544/c-wait-for-user-input – Suvarna Pattayil May 08 '15 at 13:27
  • Possible duplicate of http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input – Tony Delroy May 08 '15 at 13:29
  • 5
    As a user I would find it extremely uncomfortable when the screen is spammed while I am trying to type something. Are you really sure you want to do this? – 463035818_is_not_an_ai May 08 '15 at 13:33
  • Threads is the answer and Boost is the library. http://stackoverflow.com/questions/12437395/how-can-i-execute-two-threads-asynchronously-using-boost – LovaBill May 08 '15 at 13:35
  • Where is the assumption that he is using threads coming from? Maybe he is just writing a simple program that is supposed to infinitely loop and get the user's input and do stuff with it? He can do a `while(1){}` statement for an infinite loop if that is ALL he wants to do. But OP has not really defined the type of code he is writing. – Javia1492 May 08 '15 at 13:46
  • @tobi303 sometimes you shouldn't care at all about what user want, and do wired stuff just for learning propouse. – Gabriel Ciubotaru May 08 '15 at 13:54
  • @GabrielCiubotaru For learning purpose I like to learn things the right way :P – 463035818_is_not_an_ai May 08 '15 at 15:58
  • @tobi303 we have different approach, i'm a low lvl programmer and i usually try to make all the dummest things to learn how compiler / os / system is working – Gabriel Ciubotaru May 08 '15 at 16:19

2 Answers2

1

Because the cin and read functions in general are blocking operation, you cannot make something else because your main thread is "blocked".

The only way for doing this is to create another thread which will print to the same STDOUT controlled by a global variable. Basically you need something like:

//print thread
while(global == 1)
{
    cout<<"TEXT";
    ........
}

and in the main thread:

volatile int global = 0;
....
global = 1;
cin>>age;
global = 0;

This is not a good practice but you can start with this but try to use a MUTEX instead of the global variable

Varpie
  • 57
  • 1
  • 8
Gabriel Ciubotaru
  • 1,042
  • 9
  • 22
  • 2
    In C++, you can use [`std::thread`](http://en.cppreference.com/w/cpp/thread/thread) class to create threads and you also have ready class [`std::mutex`](http://en.cppreference.com/w/cpp/thread/mutex) to create mutexes. –  May 08 '15 at 13:36
  • @Fireho I hate C++ as whell as STL so i don't know too much of it's content :D. You can rewrite my answer and post it using these – Gabriel Ciubotaru May 08 '15 at 13:42
  • 3
    [`volatile` doesn't do what you think it does](http://stackoverflow.com/q/19684781/332733) and should not be used for synchronization or inter thread communication – Mgetz May 08 '15 at 14:07
  • @Mgetz i used volatile because some compilers will actually take once the value from memory and will not bother to retake it from RAM but will cache it (or even use the same register for it). Same for main thread, it could be possible that `global = 0` to set value just in register / cache but not in RAM – Gabriel Ciubotaru May 08 '15 at 14:12
  • 2
    @GabrielCiubotaru: But if you are accessing it from different threads then you also need to use some synchronisation (e.g. mutex) or make it a `std::atomic`, which already makes I/O guarantees ... making the `volatile` redundant in either case. – Lightness Races in Orbit May 08 '15 at 14:25
  • (In short, _actually read_ the Q&A to which Mgetz linked you, which debunks everything you just said.) – Lightness Races in Orbit May 08 '15 at 14:26
1

This worked for me

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>

using namespace std;

void *PrintWaiting(void *id)
{
   for(;;)
   {
      cout << "Waiting.............." << '\r' << flush; sleep(1);
      cout << ".......Waiting......." << '\r' << flush; sleep(1);
      cout << "..............Waiting" << '\r' << flush; sleep(1);
   }
   pthread_exit(NULL);
}

int main ()
{
   pthread_t thread;

   pthread_create(&thread, NULL, PrintWaiting, NULL);

   int age;
   cout << "Hello please enter in your age: "; 
   cin >> age; 

   pthread_cancel(thread);

   cout << age << endl;

   pthread_exit(NULL);
}

compile with -lpthread flag.

It'll look strange, you have to figure out how to format the output

dlavila
  • 1,204
  • 11
  • 25