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 14 1621
S. Nurbe wrote: 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.
--
Thomas
Thomas Matthews wrote: S. Nurbe wrote:
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.
More on-topic: So does the standard C language ;-)
Cheers
Michael 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.
-- Thomas
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
S. Nurbe <re*******@yahoo.de> wrote: 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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
On Fri, 11 Mar 2005 09:30:23 -0800, S. Nurbe wrote: 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 Je***********@physik.fu-berlin.de wrote: S. Nurbe <re*******@yahoo.de> wrote: 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.
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
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
Kiru Sengal wrote: Je***********@physik.fu-berlin.de wrote: S. Nurbe <re*******@yahoo.de> wrote: 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.
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 */
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.
Tommy Reynolds <To************@MegaCoder.com> writes:
[...] 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*.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Luke Wu" <Lo***********@gmail.com> writes:
[...] 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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Luke Wu wrote:
.... 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.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
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.
On Fri, 11 Mar 2005 21:19:56 +0000, Keith Thompson wrote: 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*.
There are equivalents nearly everywhere.
Cherry bye
Kiru Sengal <ki*********@gmail.com> wrote: Je***********@physik.fu-berlin.de wrote: 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.....
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de re*******@yahoo.de wrote: 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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vadym Stetsyak |
last post by:
Hi there!
Is there any way how can I stop garbage collection for a period of time.
Lets say, I have a form. Before form load I pause the GC and when the form
is shown resume GC
|
by: MA |
last post by:
Hi all!
I know there is other newsgroups for webservices, but I don´t get any
answers on those.
I have developed a webservice that writeing and reading files in different
folders.
Question...
|
by: Åženol Akbulak |
last post by:
Hi,
I have an asp.net application. And I have worker threads which must run
always (7x24).
I start my threads in Application_Start, and I stop its in Application_Stop
events. I log this...
|
by: Chad |
last post by:
I rebuilt my pc (format and reinstall) running XP and Visual Studio
2003.
My problem is that the IDE won't stop the run session when I close the
web application.
Now when I create a new...
|
by: Jeff |
last post by:
Hi
I have a library mde that is used with some customer databases and I found
out that another developer discovered it while doing some maintenance work
on an old database for the same customer...
| |
by: M Craig |
last post by:
I'm trying to write a custom installation engine to plug into our
existing build system. Some things I'm trying to do are,
Create/Delete/Start/Stop Application Pools, Web Sites, and Virtual...
|
by: Marc Bartsch |
last post by:
Hi,
I have a background worker in my C# app that makes a synchronous
HttpWebRequest.GetResponse() call. The idea is to POST a file to a
server on the internet. When I call HttpWebRequest.Abort()...
|
by: gdrenfrew |
last post by:
I'd like to know if it is possible to stop the parent application of a floating form becoming active when the controls on the floating form are clicked?
I've got a large application, with multiple...
|
by: =?Utf-8?B?anAybXNmdA==?= |
last post by:
I have a windows application that does not stop running whenever the
application exits.
Could someone fill me in on what I am doing wrong?
Here is the relevant code:...
|
by: =?Utf-8?B?am1hZ2FyYW0=?= |
last post by:
My program needs to do X when someone 'starts using' their Windows user
account, and it should do Y when they 'stop using' their Windows user
account. By 'starts using' I mean they log on, unlock...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |