stop application

S

S. Nurbe

Hi,

I have programmed a while loop. At the end of this loop I want to stop
it until someone hit a key. When someone hits the right key the loop
shall start again.
Actually I thought this would be easy to do, but when it waits for
input the application apparently works in the background and when I
hit the right key the loop was already processed several times instead
of starting a new loop.
Are the input and the loop working in different threads?
How can I solve this?

part of my code:

int display = 1;

while (display)
{...
display=0;
printf("\nHit 'c' to continue.\n");
input = getchar();
if (input == 'c')
{
display = 1; //start loop again
}
else display = 0;
....
}

Thanks in advance,

S. Nurbe
 
T

Thomas Matthews

S. Nurbe said:
Hi,

I have programmed a while loop. At the end of this loop I want to stop
it until someone hit a key. When someone hits the right key the loop
shall start again.
Actually I thought this would be easy to do, but when it waits for
input the application apparently works in the background and when I
hit the right key the loop was already processed several times instead
of starting a new loop.
Are the input and the loop working in different threads?
How can I solve this?

part of my code:

int display = 1;

while (display)
{...
display=0;
printf("\nHit 'c' to continue.\n");
input = getchar();
if (input == 'c')
{
display = 1; //start loop again
}
else display = 0;
...
}

Thanks in advance,

S. Nurbe

To accomplish what you want requires platform
specific code to poll the input stream to see
if there is any data there. The _standard_
C++ language implementation waits for data
to come into the stream.

I don't think there is any need for creating
threads. But, this depends on the operating
system. Ask in a newsgroup about your
platform.
 
M

Michael Mair

Thomas said:
To accomplish what you want requires platform
specific code to poll the input stream to see
if there is any data there. The _standard_
C++ language implementation waits for data
to come into the stream.

More on-topic: So does the standard C language ;-)

Cheers
Michael
 
J

Jens.Toerring

S. Nurbe said:
I have programmed a while loop. At the end of this loop I want to stop
it until someone hit a key. When someone hits the right key the loop
shall start again.

I guess you mean "continue".
Actually I thought this would be easy to do, but when it waits for
input the application apparently works in the background and when I
hit the right key the loop was already processed several times instead
of starting a new loop.

How do you know that? I really can't see why the program would be
running while it's at the same time waiting for user input. So,
could you please elaborate a bit on how you detected this or what
made you think it's doing that?
Are the input and the loop working in different threads?
How can I solve this?
part of my code:
int display = 1;
while (display)
{...
display=0;
printf("\nHit 'c' to continue.\n");
input = getchar();
if (input == 'c')
{

Here you've got a real problem. Input from the keyboard arrives
only after a whole line, with a final <ENTER>, has been typed
in. Before this happens your call of getchar() will not return.
But if when happens you will not get just a single character,
but at least two, since there's always at least the the '\n'
character that ended the line. For that reason your while loop
will normally will not run more than twice, since the second
time round there's already a character in the input buffer (the
'\n' if the user entered first 'c' and <ENTER>) which will then
be returned by the second call of getchar().

Changing this behaviour so that you get a character immediately,
i.e. without pressing <ENTER>, can only be done using system-
specific methods, so you better ask about them in a newsgroup
dedicate to discussions of the system you are using.

On the other hand, if you don't mind about pressing also the
<ENTER> key you will have to empty the input buffer after the
call of getchar(). This can easily be done using something
like

while( ( c = getchar( ) ) != '\n' && c != EOF )
/* empty */ ;

Please note that 'c' must be an integer, not just a char, or
you won't be able to check for EOF.
display = 1; //start loop again
}
else display = 0;
...
}
Regards, Jens
 
T

Tommy Reynolds

Actually I thought this would be easy to do, but when it waits for
input the application apparently works in the background and when I
hit the right key the loop was already processed several times instead
of starting a new loop.
Are the input and the loop working in different threads?

Unless you are manually creating threads, there is only one thread in
a C program. The problem is that stdin is normally line buffered.
Look up termios to see how to turn off stdio line buffering.

Cheers
 
K

Kiru Sengal

snip.


Changing this behaviour so that you get a character immediately,
i.e. without pressing <ENTER>, can only be done using system-
specific methods, so you better ask about them in a newsgroup
dedicate to discussions of the system you are using.

On the other hand, if you don't mind about pressing also the
<ENTER> key you will have to empty the input buffer after the
call of getchar(). This can easily be done using something
like

while( ( c = getchar( ) ) != '\n' && c != EOF )
/* empty */ ;

And one probably should only unleash this beast if the first character
(the one required for input) wasn't a '\n' to begin with.....

input = getchar();
if(input == EOF){
/* deal with it somehow */
}
else if(input != '\n') /* unleash the beast */
while( (c = getchar()) != '\n' && c != EOF)
; /* empty */

/* at this point, we don't know whether the last character read
into c (by the beast) was a newline or EOF- keep this in mind */


If the original input was a '\n', that means the user hit ENTER
immediately, and there isn't any goodies in STDIN for the beast to
consume (we have to keep him/her caged).

Take care
 
R

record700

Hi,

That's right. It only works twice. I solved it like this:
if (input == '\n')
{
display = 1; //start loop again
input = NULL;
}
....seems to work.
But the main problem still exists: I'm reading data from a serial port.
In the while loop I call read(). This needs 2-3 seconds until I have
enough data to display it onto the screen and I reach the point to
press a key. When I wait now 3 seconds and hit the key, to program
displays the result of the new input data directly without the 2-3 sec
to collect it. This is why I think, the while loop runs in the
background although it waits for keyboard input.
Probably the reason for this lies in the serial port definition.
Actually the program I'm using is not coded by from the beginning, I'm
only extending it, so by now I couldn't figure out, what the reason is
for this behaviour.
Here is the serial port config:

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL ;
cfsetispeed(&newtio, baudrate);
cfsetospeed(&newtio, baudrate);

tcflush(input_stream, TCIFLUSH);
tcsetattr(input_stream, TCSANOW, &newtio);

I think one has to stop the input stream but I couldn't figure out how.

Does anybody know?

Thanks
 
L

Luke Wu

Kiru said:
And one probably should only unleash this beast if the first character
(the one required for input) wasn't a '\n' to begin with.....

input = getchar();
if(input == EOF){
/* deal with it somehow */
}
else if(input != '\n') /* unleash the beast */
while( (c = getchar()) != '\n' && c != EOF)
; /* empty */

/* at this point, we don't know whether the last character read
into c (by the beast) was a newline or EOF- keep this in mind */

Why are you checking for EOF everywhere? getchar() gets input from the
interactive terminal, not a file. Only if the user provides a special
input combination, will EOF come from getchar.

Unless you program relies on EOF being a way to exit it, you don't need
to check for EOF all the time.
 
K

Keith Thompson

Tommy Reynolds said:
Unless you are manually creating threads, there is only one thread in
a C program. The problem is that stdin is normally line buffered.
Look up termios to see how to turn off stdio line buffering.

Correction: Look up termios to see how to turn off stdio line
buffering *on systems that support termios*.
 
K

Keith Thompson

Luke Wu said:
Why are you checking for EOF everywhere? getchar() gets input from the
interactive terminal, not a file. Only if the user provides a special
input combination, will EOF come from getchar.

getchar() gets input from stdin, which could be an interactive
terminal, a file, or anything else.
Unless you program relies on EOF being a way to exit it, you don't need
to check for EOF all the time.

If an EOF condition occurs on stdin (either because the user pressed
the magic key combination or because you reached the end of the disk
file), it's likely you're not going to be able to read any more input.
If you want the program to be robust, you should definitely check for
this.
 
C

CBFalconer

Luke said:
.... snip ...

Why are you checking for EOF everywhere? getchar() gets input
from the interactive terminal, not a file. Only if the user
provides a special input combination, will EOF come from getchar.

Unless you program relies on EOF being a way to exit it, you
don't need to check for EOF all the time.

Just run the program with stdin redirected, and you will rapidly
discover reasons for checking for EOF. And, believe it or not,
most users are capable of supplying the interactive EOF, even if
only by accident.
 
R

record700

How can I stop the read command from the serial port such that no more
data is read?
I turned off the line buffering (newio.c_lflag^=ICANON; ) but it takes
no effect.

Thanks.
 
T

Tommy Reynolds

Correction: Look up termios to see how to turn off stdio line buffering
*on systems that support termios*.

There are equivalents nearly everywhere.

Cherry bye
 
J

Jens.Toerring

And one probably should only unleash this beast if the first character
(the one required for input) wasn't a '\n' to begin with.....

Yes, of course. It was actually meant to go after the
if (input == 'c')
{

since in all other cases the program was supposed to bail out of the
loop anyway. Sorry I didn't make this clear enough.

Regards, Jens
 
J

Jens.Toerring

That's right. It only works twice. I solved it like this:
if (input == '\n')
{
display = 1; //start loop again
input = NULL;

That looks strange. input seems to be an int or char, so why do you
assign a NULL pointer to it=
}
...seems to work.
But the main problem still exists: I'm reading data from a serial port.
In the while loop I call read().

And with this you leave the realm of standard C. There is no read()
function in C, it is an extension, probably supplied by your OS.
This needs 2-3 seconds until I have
enough data to display it onto the screen and I reach the point to
press a key. When I wait now 3 seconds and hit the key, to program
displays the result of the new input data directly without the 2-3 sec
to collect it. This is why I think, the while loop runs in the
background although it waits for keyboard input.

First of all, I don't really understand your explanation, in-
cluding how this leads you to believe that the program would
be running somehow in the background while it's waiting in
the call of getchar() at the same time. But, second, what
read() does may depend a lot on things like how you opened the
device file for the serial port, settings for it etc. But all
of this is off-topic here and can only be reasonably discussed
in a group dedicated to discussions about prgramming fir the
operating system you are using. If you use UNIX the group
comp.unix.programmer would be a good place, if you're on
Windows there are also lots of groups for this. So I would
strongly recommend that you ask these questions there.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top