I have problem with the following code.
#include<iostream>
template <class A> void reading(A &a) {
bool x = true;
while(x) {
x = false;
std::cin >> a;
while (std::cin.fail()) {
if (std::cin.eof()) {
std::cout <<"You pressed CTRL+D\n";
std::cin.clear();
std::cin.ignore (1000000000, '\n');
}
else {
std::cout <<"Give me numbers, not letters\n";
std::cin.clear();
std::cin.ignore(1000000000,'\n');
}
x = true;
}
}
}
int main() {
double o[3];
for (double &i:o)
reading (i);
for (int j = 0; j<3; j++)
std::cout<<o[j]<<std::endl;
}
In particular, when I press ctrl+d the program enter in an infinite loop. Does someone know where is the problem and what is the solution?
My goal is to make a template function that can read any kind of data and that disable the ctrl+d during the input.