Skip to content

Instantly share code, notes, and snippets.

@martynchamberlin
Last active December 27, 2015 17:49
Show Gist options
  • Save martynchamberlin/7364709 to your computer and use it in GitHub Desktop.
Save martynchamberlin/7364709 to your computer and use it in GitHub Desktop.
Console C++ application. Gets user input and checks to see if it is a valid integer.
#include <iostream>
using namespace std;
bool isInt(char tempNumber);
int main()
{
char tempNumber[10];
while ( true )
{
cout << "Enter a number: ";
cin >> tempNumber;
bool isIntMain = true;
for ( int i = 0; i < tempNumber[i]; i++ )
if ( ! isInt( tempNumber[i] ) )
isIntMain = false;
if ( isIntMain == false )
cout << tempNumber << " Invalid Integer" << endl;
else
cout << tempNumber << " Valid Integer" << endl;
cout << endl;
}
cin.ignore();
cin.get();
return 0;
}
bool isInt(char tempNumber)
{
bool isInt = false;
for (int i = 0; i < 10; i++)
if ((int)tempNumber == i + 48)
isInt = true;
return isInt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment