473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

stopping a while loop

Hi every C one,

Which key entry (if there is any) should stop this loop?

#include <stdio.h>
int main(){
int c, getch(void);

while((c = getch()) != EOF){
printf("%c\n", c);
}
return 0;
}

--
Jean Pierre Daviau
--
http://jeanpierredaviau.com
Nov 14 '05 #1
3 2879
Jean Pierre Daviau <On**@waseno.ugh> scribbled the following:
Hi every C one, Which key entry (if there is any) should stop this loop? #include <stdio.h>
int main(){
int c, getch(void); while((c = getch()) != EOF){
printf("%c\n", c);
}
return 0;
}


Well, to be pedantic about it, we don't know. getch() is not defined by
the ISO C standard, and thus it might return anything it wants. There
might be no way for it to ever return EOF. On the other hand, it could
return EOF after every keypress.
To be less pedantic, suppose getch() works the same way as getchar().
Then the answer is "it depends on your operating system". For example,
UNIX uses ^D, Windows uses ^Z, and AmigaOS uses ^\.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #2
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Jean Pierre Daviau <On**@waseno.ugh> scribbled the following:
Hi every C one,

Which key entry (if there is any) should stop this loop?

#include <stdio.h>
int main(){
int c, getch(void);

while((c = getch()) != EOF){
printf("%c\n", c);
}
return 0;
}


Well, to be pedantic about it, we don't know. getch() is not defined by
the ISO C standard, and thus it might return anything it wants. There
might be no way for it to ever return EOF. On the other hand, it could
return EOF after every keypress.
To be less pedantic, suppose getch() works the same way as getchar().
Then the answer is "it depends on your operating system". For example,
UNIX uses ^D, Windows uses ^Z, and AmigaOS uses ^\.


In fact, there's a function getch() defined as part of the curses
package available on Unix-like systems (and probably others). The man
page (at least on one system I just checked) makes no mention of EOF.
In fact, it's likely to be used in some kind of raw mode in which it's
not possible to specify EOF (control-D probably comes through as a
literal control-D character, '\004').

I think Windows also defines a getch() function that does something
different. I have no idea whether it ever returns EOF.

To the OP: The fact that the getch() function had to be declared
within the program should have been a clue that it's not declared in
<stdio.h>, and that it's non-standard.

If you want to write portable code, use fgetc(), getc(), or getchar()
(your system's documentation should tell you how they differ). If
you're curious about getch(), that's a system-specific question; if
you can't find answers in your system's documentation, try a
system-specific newsgroup.

--
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.
Nov 14 '05 #3
Thak you.
"Keith Thompson" <ks***@mib.org> a écrit dans le message de news:
ln************@nuthaus.mib.org...
| Joona I Palaste <pa*****@cc.helsinki.fi> writes:
| > Jean Pierre Daviau <On**@waseno.ugh> scribbled the following:
| >> Hi every C one,
| >
| >> Which key entry (if there is any) should stop this loop?
| >
| >> #include <stdio.h>
| >> int main(){
| >> int c, getch(void);
| >
| >> while((c = getch()) != EOF){
| >> printf("%c\n", c);
| >> }
| >> return 0;
| >> }
| >
| > Well, to be pedantic about it, we don't know. getch() is not defined by
| > the ISO C standard, and thus it might return anything it wants. There
| > might be no way for it to ever return EOF. On the other hand, it could
| > return EOF after every keypress.
| > To be less pedantic, suppose getch() works the same way as getchar().
| > Then the answer is "it depends on your operating system". For example,
| > UNIX uses ^D, Windows uses ^Z, and AmigaOS uses ^\.
|
| In fact, there's a function getch() defined as part of the curses
| package available on Unix-like systems (and probably others). The man
| page (at least on one system I just checked) makes no mention of EOF.
| In fact, it's likely to be used in some kind of raw mode in which it's
| not possible to specify EOF (control-D probably comes through as a
| literal control-D character, '\004').
|
| I think Windows also defines a getch() function that does something
| different. I have no idea whether it ever returns EOF.
|
| To the OP: The fact that the getch() function had to be declared
| within the program should have been a clue that it's not declared in
| <stdio.h>, and that it's non-standard.
|
| If you want to write portable code, use fgetc(), getc(), or getchar()
| (your system's documentation should tell you how they differ). If
| you're curious about getch(), that's a system-specific question; if
| you can't find answers in your system's documentation, try a
| system-specific newsgroup.
|
| --
| 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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
4
by: bjm | last post by:
I am writing a program that will automate a series of application installations. I want to give the user the option of stopping the program's execution in between installations (for example, give...
4
by: Eran.Yasso | last post by:
Hi, Is there a way to stop a loop running in a thread using bool? What i mean is: i have two classes. When i click button(classA) on, i starts running s thread(classB) which is located...
10
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash. The only diff...
10
by: whodgson | last post by:
The following code snippet transforms names into telephone book foremat. while(n<35) { cin>>first>>middle>>last; if(first=="z") break; middle=middle.substr(0,1); ...
1
by: pfildes | last post by:
Hi, I am pretty new to VB.net and am having trouble figuring out how to do this. I have an app that copies data from one datasource to another. I have it set to run through a for loop for...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.