If I understood your question correctly, your problem is not how to write the code in C++, but how to start with the problem:
First of all, zero (0) is a valid value for uint32_t
, so you only have to check if the number is larger than the maximum value but not if the number is zero.
If your program uses only digits as input, negative values are not possible (because the minus sign is not a digit).
If you use strtoul
(as suggested in the other answer), you have to check errno
because that function returns the maximum value (4294967295) both if this number is specified in argv[1]
(which would be valid) and if a larger number is specified (which would be invalid).
If you want to write this program without using strtoul
, you can also use the following approach to compare two numbers that are represented as strings:
- Count the number of digits not including the leading zeroes.
The number that has more digits is larger.
- If the number of digits is the same, find the first digit that differs.
That digit will be larger in the larger number.
If all digits are equal, the number is equal.
This approach will also work for large numbers - for example numbers that have 100 decimal digits.
You simply have to compare the string argv[1]
against the string "4294967295"
this way.