I have this simple code where I input values to two float variables and they get displayed. What I am not able to understand is what happens if I give my input as non numeric. Like when I input 'a'( without ' '), a strange number gets generated- 2.69525+0323.21398e=039. How and why is this number generated?
#include<iostream.h>
int main()
{
float x,y;
cin>>x>>y;
cout<<x<<y;
return 0;
}
Also, how can I stop someone from entering a non-numeric value? I thought of storing the number in an array,iterating through it and using isdigit(), also a case for seeing if a decimal point appears and it should only appear once but this seems a not so good approach. Any better approach?
xandyare initialized with. That's why you should always initialize your variables.