Created
August 21, 2012 00:59
-
-
Save raarce/3410116 to your computer and use it in GitHub Desktop.
Command line argument example 02
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
// 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