For any program, which requires looping a few times, after around the 5th loop, the printf statement prints continuously, without waiting for scanf input. Have no idea what's wrong... it works perfectly fine on my other laptop though. And i have tried using fflush(stdin), the output is still the same.
here's an eg of my code:
#include <stdio.h>
int reverse(int n);
int main() {
int x, reversed;
while(1){
printf("input a number: \n");
scanf("%d", &x);
reversed=reverse(x);
printf("the reversed number is: %d \n\n", reversed);
}
return 0;
}
int reverse(int n)
{
int m=0;
while (n>0){
m*=10;
m+=n%10;
n/=10;
}
return m;
}
After the 5th loop, the printf statements printf continuously like a hundred times, before the program crashes.
I'm encountering the same problem with many other programs as well.
Anyone has any idea why? Is there a setting which i should change? im using microsoft visual studio 2010 express, for c++.
I would guess from the code that you're accidentally entering a character when a number is expected. Hard to say since you don't try to handle that scenario or check return values to make sure your input was successful.