473,566 Members | 3,273 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 2294
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
3253
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 *** color1="#eeeeee" Color2="#dddddd"
8
1977
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 have any idea what's causing this?
4
1578
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 before the Load event when the view state got restored. However, I have a custom control that dynamically adds a Label control inside of its...
6
2625
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 shut it down. Including how to establish the running environment( current working folder, ...etc) for each ASP .NET application. Because in my...
6
2255
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 Chipitsine
2
4067
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 happen nor when the utilities are set to execute. We only supply 'time ranges' for DB2 to operate upon. What I want is a means to 'force' a new...
16
2323
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
1489
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 during the day (or when I execute the stored procedures in db2 on the AS400 directly) everything completes successfully. However when the...
1
2456
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 experts-exchange.com however:
10
2075
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
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.