# help with a c++ program

This is a discussion on help with a c++ program within the C++ Programming forums, part of the General Programming Boards category; Hello! I have to make a program that reads two numbers a and b and then displays all the numbers ...

1. ## 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...
Thanks in advance !

2. 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.

3. 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

4. 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...

5. 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?

6. Post your current code. Do you see 111 and 919 or 1 1 1 and 9 1 9?

Jim

7. 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 " "...

Popular pages Recent additions