Solution:
Well, the thing you are willing to do is called Input-Validation in programming. Input validation is an important trick to know and understand. An invalid and irrelevant input can crash the whole system. So, checking an input before the further process is a good practice. There are a couple of ways to validate input in c++
I hope the below code snippet will help you to understand the whole process well.
#include <iostream>
using namespace std;
int main(){
int num;
cout << "Give me an Input: ";
// Error Checkin algorithim
while (!(cin >> num)){
cout << "ERROR: a number must be entered: ";
cin.clear();
cin.ignore(132, '\n'); // Discarding previous input
}
cout << "Your favorite number is: " << num << endl;
cout << "Thanks. Bye." << endl;
return 0;
}
Good Day!