Skip to content

Instantly share code, notes, and snippets.

@cdlewis
Created July 4, 2012 00:55
Show Gist options
  • Save cdlewis/3044441 to your computer and use it in GitHub Desktop.
Save cdlewis/3044441 to your computer and use it in GitHub Desktop.
Takes a number and adds five, complete with error handling
#include <iostream>
using namespace std;
int main( int argc, char *argv[] )
{
int number;
// Go forever
while( 1 )
{
// Take input
cout << "Enter any number you want!: ";
cin >> number;
// Make sure input is numeric
if( cin )
{
// Add 5
number = number + 5;
// Print output
cout << number << endl;
}
else // If the input is not numeric, show an error and clear stream
{
cout << "You didn't enter a number, silly!" << endl;
cin.clear();
cin.ignore();
}
}
return 0; // just because
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment