I have a Win32 console program that I wrote and it works fine. The program takes input from the user and performs some calculations and displays the output - standard stuff. For fun, I am trying to get the program to work on my Fedora box but I am running into an issue with clearing cin when the user inputs something that does not match my variable type. Here is the code in question:

This code works in Windows. For example, if the user tries to enter a char data type (versus double) then the program informs the user of the error, resets cin, and allows the user another opportunity to enter a valid value.

When I move this code to Fedora, it compiles fine. When I run the program and enter an invalid data type, the while loop never breaks to allow the user to change the input.

My questions are; how do I clear cin when invalid data is inputted in the Fedora environment? Also, how should I write this code so it will work in both environments (Windows & Linux)?

I think that cin.ignore sets the failed flag on cin, which makes it stay in the upmost if statement for ever. INT_MAX is a very large number - are you sure it is allowed to cin.ignore on all platforms?

I agree with Anders Abel and Johannes Schaub; I think also that it may not be guaranteed that, in case of error, principal is left untouched, so you may consider adding principal=0.0; at the beginning of the cycle.

By the way, to perform that kind of work I usually use this template function: