Thread: Problem with "Necklace" program

  1. #1
    Registered User
    Join Date
    Oct 2005

    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:

    #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;
      } while ((oldnum != num1) && (newnum != num2));
      cout << "Your numbers required " << step << "steps. \n";
    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

    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

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    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.
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Feb 2003
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM