Hi,

I'm going through loops at the moment in C. This example is simply an infinite loop which takes a value from the user and then asks do they want to continue and if so take another value and so on. When the user says no, the loop breaks and the average is calculated. My problem here is that when I run and enter a value and hit return, say 5, the return character is also used to say "yes continue".

This program calculates the average of any number of values.
Enter a value: 5

Do you want to enter another value? (Y or N):
Enter a value:

I can get around this by dealing with the return character.
scanf("%c", &answer);
to
scanf("%c %c", &answer, &something_else);

I know you have to deal with the carriage return in c++ but I don't remember having to do this in the past with c. Is this a new C99 requirement? I ask because this example is from a 2006 book Apress Beginning C from Novice to Professional (4th Edition Oct 2006). ie, code block is:

Code:
int main(void)
{
  char answer = 'N';      /* user to say yes to no to keep looping */
  double total = 0.0;     /* keep a running total of numbers */
  double value = 0.0;     /* values enter by user */
  int count = 0;          /* keep a running count of total entries */

  printf("\nThis program calculates the average of any number of values.");

  for( ;; )               /* indefinite loop */
  {
    printf("\nEnter a value: ");
    scanf("%lf", &value);
    total += value;
    ++count;

    /* check if user has any more numbers */
    printf("\nDo you want to enter another value? (Y or N): ");
    scanf("%c", &answer);

    if(tolower(answer) == 'n')
      break;              /* exit loop if answer is no */
  }

  /* output the average to 2 decimal places */
  printf("\nThe average is %.2lf\n", total/count);
  return 0;
}