Input in while loop

I want to have asyncronus input on a loop?
How can i make that?
To Be clear:
1
2
3
4
5
6
7
8
int onplus = 0;
string lel;
while(1!=0)
{
oneplus++

//here
cin >> lel;/*i want to continiously increment oneplus, but if i type something it will let me, but continiously incrementing oneplus;*/

I'm not exactly sure what you are trying to achieve here.

If you mean that you want the variable onplus to increment and not pause while you ask for input then you would need to do this in a separate thread.
I throught about that.
There's no other way?
Because c++ doesn't really support thread(well 2011 does, but i'm on Windows), and i don't want to run additional library or other stuff neither use vc..
Because c++ doesn't really support thread(well 2011 does, but i'm on Windows)

Being on Windows doesn't prevent you from using C++ 2011, its down to the compiler being compliant. Without using threading, or a additional library to do the threading for you then your pretty much stuck.
Found out, i'm currently using TDM-GCC, there is clang, but doesn't have libraries for c++11 on windows.
There are better alternatives?
If your compiler has the conio.h library available, you can include that and use the kbhit() function to check for input on the keyboard buffer before trying to read it. Aside from that, it's a bit more difficult, but you can write your own kbhit function to check for input in a non-blocking way using stdio.h.

The only issue is that if you are then using cin to read the data, your program will block until the user hits enter.

1
2
3
  if(kbhit()) { //non-blocking input check (loop continues to run)
    cin >> lel; //blocking read (your program will stop looping until the user hits enter)
  }


The only surefire way to handle it is by using threads, but I don't know much about how to use windows threads as I've only ever written thread code using the pthreads library in linux.

well if i rember well, in linux there's fork(), so it's a lot easier.
But thanks anyway, i think i will stick to threads.

EDIT: I'm reading that kbhit() is not on the ASI C++ stanard, is mainly from Borland compilers.
Last edited on
A fork is different than a thread. Fork is easier to handle yes, but it runs as a separate process in it's own memory space, so you would need another mechanism to access shared memory. Fork won't easily do what you want. Using threads you can access all of the memory spaces in your main process from all of your threads. So in your case, a thread would allow you to have a blocking read into a variable that could then be further processed by your main thread. I'm familiar with pthreads, but not with the windows thread libraries.

I believe the conio.h library is primarily available through the Borland compilers, but I don't know if it's available elsewhere. I know it isn't part of the ANSI C++ standard, but figured if you had it available it could work for what you need. Once your binary is compiled, you can run it on any system without any external dependencies anyway, so you would only need access to it at compile time.

If you don't have it available, then I guess you're either back to learning how to use threads, or finding another non-blocking read method...

Topic archived. No new replies allowed.