5 Replies - 2201 Views - Last Post: 15 January 2010 - 01:40 PM Rate Topic: -----

#1 ben104  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-May 08

Continue program while waiting for input

Posted 10 January 2010 - 03:14 PM

Hi,
I would like to know how to start a c++ program and ask for input, while having my code continue until I recieve input. I kinda want it to work like this
cin >> x;

while (x!="exit")
{
// do code while waiting for x to equal "exit"
}

Any help would be great!
Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Continue program while waiting for input

#2 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 454
  • Joined: 10-October 09

Re: Continue program while waiting for input

Posted 10 January 2010 - 03:18 PM

Just so i understand.

You want the program to continue running while the user is still deciding what input he/she would like to use?
Was This Post Helpful? 0
  • +
  • -

#3 ben104  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-May 08

Re: Continue program while waiting for input

Posted 10 January 2010 - 03:19 PM

Yeah pretty much
I need "cin" to not stop my program from running

This post has been edited by ben104: 10 January 2010 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 454
  • Joined: 10-October 09

Re: Continue program while waiting for input

Posted 10 January 2010 - 03:23 PM

sadly, i don't know how to do this, but i would love to see how this is done!

nice post ben :)



EDIT: Woot! 100th post, and hopefully, many, many more :)

This post has been edited by optix212: 10 January 2010 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Continue program while waiting for input

Posted 10 January 2010 - 10:20 PM

There are (at least) two ways to do this:

#1 Use multi-threading
#2 Use a message loop.

(actually #1 is just a fancy version of #2)

I hope that Ancient Dragon see this thread because I think he has been doing some work that could put you strait... Take look at this snippet.
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Continue program while waiting for input

Posted 15 January 2010 - 01:40 PM

have a look at this program with two threads
// example with two threads
// (1) loop read 100 mSec sleepfor value
// (2) loop sleeping for sleepfor value and display an *

#include <iostream>
#include <process.h>
#include <windows.h>


using namespace std;

int sleepfor=10;	 // holds data passed between threads - 1 second initially

// thread function - loop sleeping for sleepfor value and display an *
void myThread( void* number)
{
	cout << "thread started " << endl;
	while(1)
		{
		 Sleep(sleepfor*100);   // delay sleepfor 100mSec
		 cout << "*";		   // display *
		}
	 _endthread();

}

// main thread - read sleepfor
int main(int argc, char *argv[])
{
	 _beginthread( myThread, 0, NULL);  // create the thread
	 while(1)
		{
		cin >> sleepfor;				// read new value of sleepfor
		}
	return 0;
}


thread 1: in main() the value of variable sleepfor is read from the keyboard
thread 2: myThread() prints * characters with a delay set by variable sleepfor (in 100mSec stops)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1