Skip to content

Instantly share code, notes, and snippets.

@tjkhara
Created September 28, 2018 03:59
Show Gist options
  • Save tjkhara/0f12e3d493518b6c846aeb0b82e05214 to your computer and use it in GitHub Desktop.
Save tjkhara/0f12e3d493518b6c846aeb0b82e05214 to your computer and use it in GitHub Desktop.
Validation check if the value entered is the number 2
// Video: https://www.youtube.com/watch?v=kQIkWDf2XXE&feature=youtu.be
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
bool checkIf2(int numIn)
{
// Error checking algorithm
while(!(cin >> numIn) || (numIn != 2))
{
// Explain error
cout << "ERROR: Only 2 is an acceptable input: ";
// Clean input stream
cin.clear();
// Discard previous input
cin.ignore(132, '\n');
}
return true;
}
int main() {
//Variables
int num;
// Ask user to enter a number
cout << "Enter the number 2: ";
cin >> num;
if(checkIf2(num))
{
cout << "The number is 2." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment