# Problem with "Necklace" program

• 10-29-2005
dan1
Problem with "Necklace" program
Hey, I am a beginner to C++ and this program I have to do is rather confusing me.

Basically the concept is that you ask the user to input 2 numbers, (ie 1 and 8) and then you must add the numbers together and only save the first digit (ie 1+8 you would save the 9). Then, you must loop this process and have it that it will add the 8 and the 9, which will give you 7 (17 but you only save the first digit). This keeps on happening until the numbers 1 and 8 next to each other appear in the "necklace", and you must tell the user how many steps it took to reach back to the original 2 numbers.

If i havent explained it clear enough, heres what should be outputted: 18976392134718 , 12 steps

And here is my code:

Code:

``` #include <iostream>   using namespace std; int main() {  int num1, num2, step = 1;   cout << "Enter in a positive number \n";   cin >> num1;   cout << "Enter in a second positive number \n";   cin >> num2;   cout << num1 << num2 <<endl;   int oldnum,newnum;   oldnum = num1;  // copy of usernum   newnum = num2;     newnum = (oldnum + newnum)%10;   cout << newnum;     do {       step = (step + 1);       oldnum = num2;       newnum = (oldnum + newnum)%10;       cout << newnum;             oldnum=newnum;         } while ((oldnum != num1) && (newnum != num2));   cout << "Your numbers required " << step << "steps. \n";                       system("pause");         return(0);      }```
I do have little experience with C++ and one of the operations we were supposed to use was the % (modulus division) and as you can tell I don't really understand how it works/how to use it :p

The two main problems I am having are that I can get the 2 numbers to add to each other and then I can have the second original number add with the single digit total but after that they just give me random numbers.

The second problem is that my while statement does not seem to be fully working. Basically itll stop once num1 or num2 are produced in the calculations, not when both num1 and num2 are next to each other.

Any help would be appreciated and keep in mind I am a beginner with C++ code so any explanations without some of the more adv. code would be appreciated. Thanks
• 10-29-2005
major_small
well, the modulus operator returns the remainder of an integer division. for exmaple, if you do 5%2, you get 1 (2 "goes into" 5 twice, with a remainder of 1).

basically, you need to use that to figure out how many digits are in your number. then you can divide by 10 to the power of one less than the amount of digits in the number to bring that number down to a single number with a decimal. a simple integer cast gets rid of that problem.

here's how it works...
1. you get a number (we'll use 314159)
2. use modulo to find how many digits are in the number (6)
3. divide the number by 10 to the number of digits minus one (314159/(10^(6-1))) or (314159/100000)
4. now you have the first digit followed by the rest of the number as a decimal (3.14159)
5. a simple cast will fix that (static_cast<int>(num))

note: that last cast may not be necessary if you're only using integers, but it helps readability of your code. I'm going to leave figuring out how to use the modulus operator to you... there are, of course, many ways to do this.
• 10-29-2005
R.Stiltskin
Based on your example, I think you meant to say that the user must enter two single-digit numbers. Also, after the addition, you are keeping the LAST digit, not the first: I would call 7 the last digit of 17, wouldn't you?

If so, I don't see anything wrong with the way you are using "%", and your use of C++ is not the problem. The problem is in the logic of your program, & it won't do you a lot of good for other people to debug that for you. Try "running" your program with yourself as the computer & keep track of the value of each variable on paper & you will see where it's going wrong.