473,388 Members | 1,355 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,388 software developers and data experts.

stopping a loop

I have been struggling with this ever since my first post. I need to break this loop by way of too many integers entered.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main ()
  3. {
  4. /* variable definition: */
  5. int count, value, sum; double avg;
  6. /* Initialize */
  7. count = 0;
  8. sum = 0; avg = 0.0;
  9. // Loop through to input values
  10. while (count < 5)
  11. {
  12. printf("Enter a positive Integer\n");
  13. scanf("%d", &value); 
  14.  if (value >= 0) 
  15.  { sum = sum + value; count = count + 1; } 
  16.  else { printf("Value must be positive\n"); }
  17. }
  18. // Calculate avg. Need to type cast since two integers will yield an integer
  19. avg = (double) sum/count;
  20. printf("average is %lf\n " , avg );
  21. return 0;
  22. }
I don't really understand what the count = count + 1 means either.
Mar 27 '16 #1

✓ answered by weaknessforcats

There's nothing you can do to stop a loop based on data entry by the user. The reason is that while the data entry occurs, your program is not running. That is, a scanf(%d, &value) does not force the user to enter anything specific. When scanf runs all it does is interpret data already entered on sysin.

Worse, if you find that there are more than five integers in sysin, there's no way to flush the input buffer because that buffer is not yours to control. It belongs to the system. BTW: You can flush output buffers because those you created yourself and therefore are yours to control.

Editing user input is a major programming effort. scanf is just intended to get you started and not meant for any serious work. My advice while you are learning is to use friendly input at all times. If you insist on 5 integers, then be sure you enter 5 integers.

2 3405
Luuk
1,047 Expert 1GB
If I may answer your last question, what the count=count + 1 means:
The value of the variable 'count' is assigned with the value of 'count + 1'
(or, in other words, count is incremented by 1)


You also may do some googling on 'programming c course'
Mar 28 '16 #2
weaknessforcats
9,208 Expert Mod 8TB
There's nothing you can do to stop a loop based on data entry by the user. The reason is that while the data entry occurs, your program is not running. That is, a scanf(%d, &value) does not force the user to enter anything specific. When scanf runs all it does is interpret data already entered on sysin.

Worse, if you find that there are more than five integers in sysin, there's no way to flush the input buffer because that buffer is not yours to control. It belongs to the system. BTW: You can flush output buffers because those you created yourself and therefore are yours to control.

Editing user input is a major programming effort. scanf is just intended to get you started and not meant for any serious work. My advice while you are learning is to use friendly input at all times. If you insist on 5 integers, then be sure you enter 5 integers.
Mar 28 '16 #3

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

Similar topics

11
by: Wayne Folta | last post by:
Two observations about PEP-315: 1. It's clever, addresses a definite "wart", and is syntactically similar to try/except. But it's syntax seems like an acquired taste to me. 2. It is a very...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
9
by: rbt | last post by:
What is the appropriate way to break out of this while loop if the for loop finds a match? while 1: for x in xrange(len(group)): try: mix = random.sample(group, x) make_string = ''.join(mix)...
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...
2
by: alxasa | last post by:
Hi, I have a setInterval which executes its command every 10 seconds in a infinite loop. I've got something real basic like: var processes=0; function startme(){ if(stopthisloop>1)
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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 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.