I'm trying to range check a user supplied command line parameter in a C++ program and I'm not sure how to do it.
If I enter a number that's higher, it gets converted to a lower number.
What's the preferred method to do the below? Is there a constant that I can reference? And if so, is there a better way to do this range checking?
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
if (argc < 2) {
cout << endl << "usage: " << argv[0] << " <port number>" << endl << endl;
return 1;
}
int32_t port = atoi(argv[1]);
if (port < 1 || port > 65535) {
cout << endl << "port number should be between 1 and 65535" << endl << endl;
return 1;
}
cout << "port: " << port << endl;
return 0;
}