I'm attempting to write a function that scans a number from the user as a string and converts the string to an array of int type, then reverses the order of the array and puts zeroes where no scanned data exists (eg. program says "Enter a number up to 5 digits" user imputs 22, if working properly, the function should scan the number 22 as a string, and reverse the order so, n[0]=2,n[1]=2,n[2]=0,n[3]=0,n[4]=0. ) My issue is creating the zeroes I know that if nothing is scanned in by the user, some random number will be stored in the memory slot (for lack of a better term) so I wrote this for loop in hopes of obtaining my zeroes:
for(i=0;i<MAX_DIGITS;i++)
{
n[i]=num[i]-'0';
if(n[i]>9 or n[i]<0)
n[i]=0;
cout<<"n["<<i<<"]"<<n[i]<<"\n";
}
where num is the string scanned by the user, and n[] is my int array. Here's my problem, the number 9 is always the "random number" stored in the position num[3] the second time i call the function if the string is less than 4 digits, so my "if" condition doesn't catch it. is there a better way to approach this?
ps- MAX_DIGITS is a global constant