I have only been learning C++ for a few weeks and I have run into a problem trying to write a simple program. I am trying to write a program that converts F to C temperature. I tried to use a "Do..While" loop to make sure the user was entering a valid temperature. Everything works fine as long as I put in a valid integer. As soon as I enter a character it crashes. The debugger says "An access violation (segmentation fault) raised in you program." I have been searching the internet for two days trying to see what I did wrong. What did I do wrong? Please help!
Instead of making a do while, you should just use a while loop that checks for the cin.fail(), then within that while loop let the user re-enter their choice.
It should work after that!
Or change the if (cin.fail ()) to while (cin.fail ()) at line 26. Add another cin >> fahren; in there after the error message.
Then the original outer loop is unnecessary. I'm not sure how that originally could have worked anyway, since the cin.clear(); at line 28 ensures that the fail() condition could never be true at line 33. However when I tested the code, I did not get any crash or access violation - it simply output some default value of fahren and the celsius value, but ended cleanly.