This is part of my input value, what i want to do is to only input 0-9 however when i put an alphabet or any invalid key, they program works fine it ask to re enter.
invalid input please re-enter:
however this time when i re-enter it print out:[ 6.95324e-310 2 3 4 5 ]
here are the code:
int main()
{
int aSize=5;
double aArray[aSize];
double value;
for(int i=0;i<aSize;i++)
{
cout<<"enter value of slot"<<i+1<<": ";
cin>>value;
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"invalid input please re-enter: ";
cin>>value;
}
else
{
aArray[i] = value;
cout<<"value of aArray: "<<aArray[i];
}
if (cin.fail())is not sufficient. Use awhile(cin.fail())instead and get rid of yourelsebecause I suppose you want to print the value as soon as you succeed in any case.