Created
July 4, 2012 00:55
-
-
Save cdlewis/3044441 to your computer and use it in GitHub Desktop.
Takes a number and adds five, complete with error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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