Skip to content

Instantly share code, notes, and snippets.

@raarce
Created August 21, 2012 00:59
Show Gist options
  • Save raarce/3410116 to your computer and use it in GitHub Desktop.
Save raarce/3410116 to your computer and use it in GitHub Desktop.
Command line argument example 02
// Command line argument example #02
#include <iostream>
using namespace std;
bool isNum(char *st) {
int i = 0;
while (st[i] != '\0') {
if (st[i] < '0' || st[i] > '9') return false;
i++;
}
return true;
}
int main ( int argc, char *argv[] )
{
int N = 0;
int a = 0;
if ( argc < 2 ) {
cout<<"usage: "<< argv[0] <<" <integer>\n";
exit(1);
}
else if (!isNum(argv[1])) {
cout<<"parameter: "<< argv[1] <<" is not a valid number.\n";
exit(1);
}
N = atoi(argv[1]);
for (int i=1; i<N; i++) a += i;
cout << "Sum is " << a << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment