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