The C standard library function strtoul takes as its third argument the base/radix of the number system to be used in interpreting the char array pointed to by the first argument.
Where am I doing wrong?
nc=strtoul(n,&pEnd,1);
You're passing the base as 1, which leads to a unary numeral system i.e. the only number that can be repesented is 0. Hence you'd get only that as the output. If you need decimal system interpretation, pass 10 instead of 1.
Alternatively, passing 0 lets the function auto-detect the system based on the prefix: if it starts with 0 then it is interpreted as octal, if it is 0x or 0X it is taken as hexadecimal, if it has other numerals it is assumed as decimal.
Aside:
- If you don't need to know the character upto which the conversion was considered then passing a dummy second parameter is not required; you can pass
NULL
instead.
- When you're using a C standard library function in a C++ program, it's recommended that you include the C++ version of the header; with the prefix
c
, without the suffix .h
e.g. in your case, it'd be #include <cstdlib>
using namespace std;
is considered bad practice