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

I don't think there's any "random numbers" being stored. Keep in mind that the array num[] does not get cleared out every time you read in a new string. It simply get overwritten from the 0 position for as many characters (including the terminating NULL) as needed.

So, if on your first run you enter 12345 (I hope you sized the num[] array to six elements!) the array will contain the characters 1 2 3 4 5 \0. On your next run, user enters 765. The array contains 7 6 5 \0 5 \0. How does your code handle this?

Val

Well this is what i have written:

``````int main()
{
int numbr1[MAX_DIGITS], numbr2[MAX_DIGITS], sum[MAX_DIGITS];
cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: ";
return 0;
}

{
string num;
int i,x,j=0;
cin>>num;
for(i=0;i<MAX_DIGITS;i++)
{
n[i]=num[i]-'0';
cout<<"nbefore["<<i<<"]"<<n[i]<<"\n"; //n[i] before my if condition
if(n[i]>9 or n[i]<0)
n[i]=0;
cout<<"n after["<<i<<"]"<<n[i]<<"\n";//n[i] after my if condition
system("PAUSE");//added system pause to see what was happening line by line
}
cout<<endl;
for(i=0;i<=(MAX_DIGITS-1)/2;i++)
{
x=n[i];
n[i]=n[MAX_DIGITS-1-i];
n[MAX_DIGITS-i-1]=x;
}
for(i=0;i<MAX_DIGITS;i++)
{
cout<<"n["<<i<<"]"<<n[i]<<"\n"; //after reversing order
}
for(i=0;i<MAX_DIGITS;i++)
{ while(n[i]==0)
{
i++;
j=j+1;
}
}
cout<<j<<endl;
for(i=j;i<MAX_DIGITS;i++)
{
n[i-j]=n[i];
}
for(i=MAX_DIGITS-j;i<MAX_DIGITS;i++)
{
n[i]=0;
}
for(i=0;i<MAX_DIGITS;i++)
{
cout<<"n["<<i<<"]"<<n[i]<<"\n";// after rev. order and shifting array "j" positions
}

}
``````

and this is my output:

``````Please enter a number up to 5 digits: 123
nbefore[0]1
n after[0]1

nbefore[1]2
n after[1]2

nbefore[2]3
n after[2]3

nbefore[3]-48
n after[3]0

nbefore[4]-42
n after[4]0

n[0]0
n[1]0
n[2]3
n[3]2
n[4]1
2
n[0]3
n[1]2
n[2]1
n[3]0
n[4]0
Please enter a number up to 5 digits: 2
nbefore[0]2
n after[0]2

nbefore[1]-48
n after[1]0

nbefore[2]19
n after[2]0

nbefore[3]9
n after[3]9

nbefore[4]-45
n after[4]0

n[0]0
n[1]9
n[2]0
n[3]0
n[4]2
3
n[0]0
n[1]2
n[2]0
n[3]0
n[4]0
``````

aaaaand I'm really bad at this, I just discovered SEVERAL flaws in my logic (eg- the string 100 for one) I have no idea what i'm doing anymore

Ahh, now I see a big problem. In readBig, you take the input as a string type. When I run your code (after fixing a couple thing that kept if from compiling in VC++ 2005) the program would crash on a 3 digit input.

For the string, accessing any characters past the input is undefined. Well, many implementations do tack on the NULL terminator ('\0', which you see as the value -48 after the subtraction), but past that you're in no-man's-land. Change your declaration `string num;` to `char num[6]` and the program works fine.

Val

why 6? sorry if that's a dumb question...

You have to remember the extra slot for the null terminator '\0'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.