Вот ваше решение:
#include <climits>
#include <iostream>
using namespace std;
int main() {
int a, b;
char s[11]; // <-- Size should be 11, the last character will be '\0'
cin >> a;
cin.ignore(); // better change it to cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.getline(s, 11); // <-- this line sets failbit if the input exceeds 10 characters
// add these lines :
if (!cin) { // <-- checks if failbit is set
cin.clear(); // <-- clears the set flags
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // <-- ignores the whole line
}
// if anything had gone bad, it has been fixed by now
cin >> b;
cout << "a = " << a << "\ns = " << s << "\nb = " << b;
}
Более сложный, но лучший:
#include <climits>
#include <iostream>
using namespace std;
int main ()
{
cin.exceptions(ios_base::failbit|ios_base::badbit); // <-- tells compiler to treat failbit and badbit as exceptions
int a, b;
char s[11];
cin >> a;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
try {
cin.getline(s, 11);
} catch (ios_base::failure &e) {
// cerr << e.what() << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cin >> b;
cout << "a = " << a << "\ns = " << s << "\nb = " << b;
}