By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

stopping a loop

P: 12
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.

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,031
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
Expert Mod 5K+
P: 9,197
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

Post your reply

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