Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with scanf in Eclipse / MiniGW

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;
}
like image 462
Tyler Brock Avatar asked Sep 12 '26 01:09

Tyler Brock


2 Answers

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

like image 170
unwind Avatar answered Sep 14 '26 17:09

unwind


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?"

like image 21
Tyler Brock Avatar answered Sep 14 '26 16:09

Tyler Brock