views:

527

answers:

3

I'm trying to run the following code in eclipse but the console remains blank until i stop the program at which point the output "Enter next value (<=0 to quit)2130567168 minutes is 35509452 hours, 48 minutes." is repeated over and over.

It seems that scanf is putting some default value in for some reason... can't figure out why. I'm not seeing anything before the program is stopped so i thought it might have to do with printf not being flushed, but I made sure to use \n to force a flush.

Any ideas?

#include <stdio.h>
const int MIN_PER_HOUR = 60;  // minutes per hour

int main(void)
{
 int hour, min, left;

 printf("Convert minutes to hours and minutes!\n");
 printf("Enter the number of minutes (<=0 to Quit):\n");

 scanf("%d", &min);    // read number of minutes

 while(min > 0){
  hour = min / MIN_PER_HOUR; // truncated number of hours
  left = min % MIN_PER_HOUR; // number of minutes left over

  printf("%d minutes is %d hours, %d minutes.\n", min, hour, left);

  printf("Enter next value (<=0 to quit)");
  scanf("%d", &min);
 }
 printf("Done!\n");

 return 0;
}
+1  A: 

Eclipse's terminal emulator might be different and do more buffering. Try calling fflush(stdout); between the printout and the call to scanf().

unwind
That works! Now the lines a being printed and the question is being asked but typing a response doesn't make the program continue.
Tyler Brock
@Tyler: Glad it helped. You might consider accepting this answer if you feel it solved your problem (click the check mark next to my answer). Also, you've posted an answer which is more like a comment; consider removing it.
unwind
unwind, thanks for the response and suggestion. Your quick answer was great.
Tyler Brock
+1  A: 

I moved the code into visual C++ and it works as expected. I guess my questions should have been: "Why is the terminal emulation in eclipse broken?"

Tyler Brock
+1  A: 

If there's an invalid character for representing integers in the input for scanf(), the function will stop at that character, leaving it in the buffer, ready to be read again and again, and again ...

Suppose you entered "14 mins" for the first scanf(). The function reads "14" and stops at the space. Then it assigns 14 to the variable min and the program continues.

Then, inside the loop, the program executes another scanf(). This time the input buffer already has stuff -- namely " mins" with the space. scanf() will read and ignore the space, find an 'm' which it can't convert to an integer and stop without assigning a new value to the variable min and returning a value of 0. And again ... and again ... and again.

So, what you have to do is to clear the input buffer after every scanf().

How much of it do you clear?
Probably as far as the ENTER -- which is about the same fgets() does :)
For example:

int ch;
scanf("%d", &min);
do { ch = getchar(); } while (ch != '\n'); /* empty buffer */
pmg
Thanks, this is very informative but not really why it wasn't working. I hadn't typed anything yet.
Tyler Brock