Tyler Brock
Tyler Brock

Reputation: 30136

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;
}

Upvotes: 3

Views: 15983

Answers (5)

ECLIPSE Emulator should be different and do more buffering.

Try calling fflush(stdout); in between printf(); and scanf(); in each time.

It might be helpful for you.

Upvotes: 0

Sarath P
Sarath P

Reputation: 21

Problem: console gets blank when run code,again try to run gets an error.

Answer: First, open Task Manager (press alt + ctrl + delete keys) and go to details (in Windows 10). Find your file in the list, right-click on that file and click "End Task".

Second, use fflush(stdout); between every printf and scanf statement.

E.g.:

printf("Enter number 1");
fflush(stdout);
scanf("%d",&a);
printf("Enter number 2");
flush(stdout);
scanf("%d",&b);

Upvotes: 1

pmg
pmg

Reputation: 108938

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 */

Upvotes: 1

Tyler Brock
Tyler Brock

Reputation: 30136

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

Upvotes: 1

unwind
unwind

Reputation: 399881

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

Upvotes: 5

Related Questions