473,498 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to end the cycle

3 New Member
Expand|Select|Wrap|Line Numbers
  1. 1 #include <stdio.h>
  2.   2         
  3.   3 void main() 
  4.   4         
  5.   5 {       
  6.   6     unsigned int i;
  7.   7     int c;
  8.   8           
  9.   9     printf("\nThis is a test start...\n");
  10.  10 
  11.  11     c=getchar();
  12.  12     while(c!=EOF){
  13.  13         putchar(c);
  14.  14         c=getchar();
  15.  15     }
  16.  16 
  17.  17 
  18.  18 
  19.  19     printf("This is the end of the test. \n");
  20.  20 
  21.  21 }
what should i input to end the cycle?
please help ,thanks.
Jul 18 '11 #1
4 2286
Banfa
9,065 Recognized Expert Moderator Expert
You will never get EOF from stdin because all the input functions wait for input if there is current none in the buffer. The stream never ends, or more accurately only ends when the program closes.

However you have not given an accurate enough (or any in fact) description of what "the cycle" is or what your program is meant to do. You should halt the look when the program is completed its purpose is the best I can say from the available information.
Jul 18 '11 #2
dragon fishy
3 New Member
Thanks a lot .You answer is good enough to help me out,and I appriciate your patience and kindness to make such an precise description.I wish you can understand my meaning,because I am a Chinese boy for whom Enlish is not that easy.Thank you again.
Jul 18 '11 #3
Banfa
9,065 Recognized Expert Moderator Expert
OK well this is a simple program, it looks like to are just playing around while learning which is fair enough.

What your program basically does is read a character from the keyboard and write that character to the screen, repeatedly. You need to understand how the input stream works a little. Data does not (normally) enter the input stream as soon as the user presses a key but rather all the data the user has entered, including the new line, is put on the input stream when the user presses "Enter".

So how are you expecting the user to enter data? Are you expecting them to enter a few characters and press enter repeatedly, for example as if entering a list of numbers

4
356
34
64

Or are you expecting the user user to just enter a line of text with only a single press of "Enter"?

These are 2 distinct cases (and there are others) and require different handling. The second case is the simplest, the user is expected to enter a single line of data followed by "Enter". The defining feature of this is that "Enter" is pressed once so you can look for that in you input stream. Your loop condition can be c != '\n' i.e. as long as the character end is not the newline character, generated by the "Enter" key, then continue.

On the other hand the first example, entering a list of numbers is more complex. There is no defining feature, there is nothing that occurs only once at the end of the list. The normal way to get round this is to use a sentinel character, that is a character that is defined for the specific purposes of the program as indicating the end. In the case entering a list of numbers you could use the character q, the program will except a list on numbers up until it reads the character q and then it will quit. The input would look something like

4
356
34
64
q

and the loop condition might look something like c != 'q'.
Jul 18 '11 #4
dragon fishy
3 New Member
Ok.I can undertand what you said and I know the method can totally fix the problem of endless cycle.The program is from the book 《The C Progamming Language》,and I just test it and that is all.

I supposed there is a charater I can input to end the loop( Of cause I konw this can not happen now) because I thought there can be a character equal 'EOF' (ie -1). According to what you said ,now I know stdio can not do that.I will study further into the input stream staff.Thanks for explaining for me.

O(∩_∩)O~

Tell you a joke,I even input '-1' to expect the loop to come to an end...

wish you a good night.

sorry ,I should say wish you a good say.because the time zone is diffrent
-_-|||
Jul 18 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3249
by: mike | last post by:
I'd like to have the options for my cycle loaded from the config file if possible. So far I haven't been able to find the right syntax or an example, any ideas? ie <code> *** config file ***...
8
1972
by: John Ortt | last post by:
Hi there, I have written the following code to colour cycle the background in one of my forms. The problem is it only cycles from Black to Red and then reverts back to Black. Does anyone...
4
1573
by: Pham Nguyen | last post by:
I have two maybe related questions about view state and the life cycle of controls: 1) When does the view state in a control get restored? I thought there was a LoadViewState event that occured...
6
2617
by: jim | last post by:
Hi All, I like to know the life cycle of an ASP .NET Application( incudieng server application, such as .NET Web Service). That means from initialization to fully running and how to reboot it or...
6
2245
by: Ilia Chipitsine | last post by:
Dear Sirs, I want to dump all databases, but separately each database in its own file, not all databases in one single file as pg_dumpall does. How can I implement that ? Cheers, Ilia...
2
4057
by: ladino | last post by:
Hi: I'm testing DB2's new automated maintenance feature and, so far it seems to accomplish what it is instructed to do, but we (DBA's) have no control on when the evaluation cycle is going to...
16
2309
by: bharathi.vempati | last post by:
hi Can any one explain me the life cycle of C from source code to process termination. please make it fast k bye -ramya
0
1477
by: jer006 | last post by:
Hi, This is more of a general question... I have a series of stored procedures (chain of procedures) in db2 on an AS400 which are executed from a SQL Server Job, when I execute the job manually...
1
2452
by: ywoolf | last post by:
Trying to figure out how to auto-cycle an iFRAME. Here is the link in question: http://pushingtheenvelope.ca/new/web.html Would like to cycle the iFRAME choices. Got this from...
10
2064
by: fdu.xiaojf | last post by:
Hi all, I have the following code: for i in generator_a: # the first "for" cycle for j in generator_b: if something_happen: # do something here ..., I want the outer cycle to break break
0
7004
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...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
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...
0
7379
tracyyun
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...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4593
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...
0
3095
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...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
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...

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.