My target is to validate input value before add it into array. Current code used:
int main()
{
int temp;
int arr[5];
for(int i = 0; i < 5; i++)
{
// validate here
cin >> arr[i];
}
return 0;
}
and my validation method:
int validateInput(string prompt)
{
int val;
while (true)
{
cin.clear();
cin.sync();
cout << prompt;
cin >> val;
if (cin.good() && val >= -50 && val <= 50)
{
break;
}
else
cin.clear();
cout << "Invalid input! number must be between -50 and 50" << endl;
}
return val;
}
How is that possible?
cin >> Foo(arr[i], -50, 50). Google "overloading istream >>". Alas, the boring simple way is probably more practical.std::vector. Arrays have the possibility of buffer overflow, and are difficult to pass to functions (compared to vectors).