You could read the string in its entirety first and then validate it:
int main()
{
using namespace std;
const int maxchar = 5;
string nationname;
cout << "What is the name of your nation?" << endl;
cin >> nationname;
if (nationname.size() > maxchar)
{
err << "The input is too long." << endl;
return 1;
}
// ...
}
Incidentally, you should use std::endl
with your prompt and not just \n
; you want to make sure that the prompt is flushed to the screen before you start waiting for user input. (Edit: As Loki Astari points out in the comments, this part's not actually necessary.)