Zebra0.com

cpp errorsReading integers

Reading integers

When you are learning to program, a typical program is to read in some numbers and do some operations with them.

In practical applications, that is not a good idea. The user may enter a $ or other value that is not numeric. When that happens, the user doesn't know they have made a mistake but may get strange results.

It is much safer to read in a string and convert the string to an integer using stoi (string to int: pronounce as S-to-I). If the conversion fails, we can catch the error and handle it in an appropriate way.