# help with a c++ program

• 01-09-2012
Sky_Daughter
help with a c++ program
Hello!
I have to make a program that reads two numbers a and b and then displays all the numbers that can be formed by replacing the first and the last digit of b with every single digit of a.
For example,if a is 19 and b = 913 it should display the numbers:
113,911,919,913.

There is my piece of code :
Code:

```#include<iostream> using namespace std; int main() {   int a,b,i = 0,j = 0,na = 0,nb = 0,va[12],vb[12];     cout <<"a = ";   cin >> a;     cout <<"b = ";   cin >> b;     while (a) {         va[i] = a%10;         na++;         i++;     }         while (b) {         vb[j] = b%10;         nb++;         j++;     }       for (i = na-1;i >= 0;i--) {             vb[0] = va[i];             vb[na] = va[i];     for (j = nb - 1; j >= 0;j--){             cout << vb[j] << " " ;       }       }  return 0; }```
I used 'na' and 'nb' to calculate the number of digits of a and b. Also,I used 2 arrays 'va[12]' and 'vb[12]' to hold the values of every digit in the number.
What's wrong? It doesn't work properly...
• 01-09-2012
rags_to_riches
Code:

```while (a) {         va[i] = a%10;         na++;         i++; }```
When does a become 0 in this loop, causing it to terminate?

Code:

```while (b) {         vb[j] = b%10;         nb++;         j++; }```
Likewise with b.
• 01-09-2012
jimblumberg
In the following code:
Code:

```  while (a) {         va[i] = a%10;         na++;         i++;     }```
When will this loop end? When will "a" ever be zero?

Jim
• 01-09-2012
Sky_Daughter
Quote:

Originally Posted by jimblumberg
In the following code:
Code:

```  while (a) {         va[i] = a%10;         na++;         i++;     }```
When will this loop end? When will "a" ever be zero?

Jim

Of course,it was so obvious. I forgot the a/=10. I usually don't make these kind of mistakes but this time I just couldn't figure it out. I thought that there was a/=10.
Sorry for the dumb question.I'm feeling a little embarassed right now...
• 01-09-2012
Sky_Daughter
I've just run my program and it displays 111 and 919 instead of 113,919,911 and 913. How should I fix this problem?
• 01-09-2012
jimblumberg
Post your current code. Do you see 111 and 919 or 1 1 1 and 9 1 9?

Jim
• 01-09-2012
Sky_Daughter
Quote:

Originally Posted by jimblumberg
Post your current code. Do you see 111 and 919 or 1 1 1 and 9 1 9?

Jim

No. Actually,I see 1 1 1 and 9 1 9. What should I do? It's because of that space " "...