Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed for while loop? 1

Status
Not open for further replies.

sergelaurent

Technical User
May 30, 2005
52
0
0
FR
I have got the following code:

while{toto==1}
{
//instructions
}

In fact, I want to execute all the instructions in the while loop until the user click on any button of the keyboard.

I want to change to state of the variable toto from 1 to 0 when a click is perform or vice versa.

How can I do this?
 
Are you doing this in a terminal?
The problem is that terminals buffer the input until you press return.
Another problem is that getchar() function is blocking.

Here is some initialization code:
Code:
	struct termios ts;
	struct termios new_ts;
	
	{
		char c;
		ioctl(0, TCGETS, &ts);
		new_ts = ts;
		new_ts.c_lflag &= !ICANON;
		new_ts.c_lflag &= !ECHO;
		ioctl(0, TCSETS, &new_ts);
	}
	
	/* set file to non-blocking */
	{     
		int flags = fcntl(0, F_GETFL, 0);
		fcntl(0, F_SETFL, flags | O_NONBLOCK);
	}

And here is your code:
Code:
while{toto==1}
{
  char c;
  int r;

  r = read (0, &c, 1);
  
  if(r <= 0)
  {
    //instructions
  }
  else
    toto=0;
}

Hope this helped....
 
_kbhit() is what you want. The code might change into something similar:

while (!_kbhit())
{
// do stuff
}


When you do it, do it right.
 
toggle the toto

void function ()
{

while (!_kbhit())
{
if (toto == 1)
// do stuff
}


if (toto == 0)
toto = 1;
else
toto = 0;

}

tomcruz.net
 
> In fact, I want to execute all the instructions in the while loop until the user click on any button of the keyboard.
You need to say which OS and compiler you're using.

Standard C doesn't provide such an instant reaction to "press a key".


--
 
> Standard C doesn't provide such an instant reaction to "press a key".

And non-standard C or C++ does? _kbhit() is sure to be found with most compilers of C (cant tell for UNIX style, but probably there should be something similar too). And it does not react to "press a key", but sure tells you that "one was pressed".

------------------
When you do it, do it right.
 
> And non-standard C or C++ does?
Sure, why not. Who knows what functions a particular compiler vendor decided to implement for you.

And even if two different implementations choose to implement a function called kbhit(), there's no guarantee that they would do the same thing in both cases.

The point is, you can't just go round saying "use kbhit()" without actually having any idea which OS/Compiler the original poster is using.

> _kbhit() is sure to be found with most compilers of C
You have a very poor idea of what constitutes "most" then.
I use several compilers on a daily basis, and this function isn't in any of them.

Even if the compiler provides such a function (say something made by microsoft), if the original poster is writing a GUI application, then kbhit() is still the wrong thing to be choosing.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top