no idea why every single program crashes during loop ( C programming)

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++.

thanks!
I tried your code and didn't experience the same thing. It worked for me.

I'm not great at C's stdio, but you may need to have use getch() in conio to handle the "Enter" keystroke.
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.
Topic archived. No new replies allowed.