If a is an int, then cin>>a will read in characters from input until it can no longer use the characters to form a valid integer. In this case, it reads in 2 but stops at the '.' because you can't have a decimal point in an integer.
If you want a number that contains decimals, change a to be a double instead of an int. If you want your program to fail when it asks for an int but the user types a number with a decimal, you'll have to add another tweak:
That code reads into a, and then checks to see if there are any other characters after the integer. If the user types 2.45, then the cin.get() will get the decimal point, which isn't '\n'. If the user types in 68sdf8a6s, then it will read 68 into a and then fail because 's' is not '\n'. If they do it correctly and type 239, then cin.get() will get the newline from when they hit <enter>, and the read will succeed.
if ((cin >> a) && (cin.get() == '\n'))
thank you so much sir it works...
What if they type 239 followed by a space?
Originally Posted by Daved
Then the program will sit there waiting for them to hit <enter>, since input from the console isn't available to the program until <enter> is hit. At that point, the input would be rejected because "239 " is not a number.
Obviously it's not perfect, but for most of these beginner programs it's more than enough.