I have written some code, here is a snippet of it is:
int num[8],n=0;
for (n = 0; n<8; n++)
{
char temp = binnum[n];
num[n] = atoi(&temp);
cout << num[n];
}
It doesn't gives any error, but I do get a warning. When I run it on C++, it gives Run Time Check Failure - The variable n is being used without being initialized
.
After that, it doesn't run any further and the program closes. Is there any way to ignore this error? Because if I initialize n
, it gives the wrong answer. For example, if answer is 101011, it will give 10101100, which is wrong.
Your main problem (after all the edits) is that
atoi
takes a null-terminated char array (C-style string). The address of a single char variable does not make a C-style string.To convert a single character in range ['0'...'9'] to a corresponding number use:
possibly having checked that temp contains a digit character.