Stuck on Practice work...

Im trying to get my finished work to look like this: http://img.photobucket.com/albums/v323/Monkeyman1010/help.gif

These are the directions...
1. Coding the main function. Add the code for Steps 4 through 6 to function main (lines 8 - 12).

2. Defining a variable to contain user input. In lines 10 - 11, insert a full-line comment followed by a definition for an int variable number that will store user input. Then use cout to prompt the user to enter a 4-digit integer, followed by a statement using cin to input the value that the user enters into the number variable.

3. Extracting the digits from the user input. Use the programming techniques you used to solve Exercise 3.17 to insert statements that extract the digits from int variable number. Assign the digits of number to the int variables digit1, digit2, digit3 and digit4, respectively.

4. Encrypting each digit and display the encrypted results. Replace each digit by performing the calculation (the sum of that digit plus 7) modulo 10. We use the term modulo (mod, for short) to indicate that you are to use the remainder (%) operator. Swap the first digit with the third, and swap the second digit with the fourth. Display a label for the encrypted digits, then display each digit separated by a space character (Fig. 4.39). [Note: Once a number is encrypted, it will need to be decrypted in the future. You might consider how to write an application that will decrypt these values.]

5. Save, compile and run the completed application

I am stuck on step 4. Can anyone help me out?
Post what youve done so far
#include <iostream>

using namespace std;

int main()
{
int number;

cout << "Enter four-digit number: ";
cin >> number;


return 0;

Well I know this is how it starts, lol. Im confused on step three where it says "Assign the digits of number to the int variables digit1, digit2, digit3 and digit4, respectively."
You should use [code][/code] tags. Paste your code in between them so it gets formatted properly. Nothing is more annoying (other than homework questions where the OP hasn't even tried the question) than 300 lines of code without code tags >:O

Anyway as you've at least tried and are asking specific questions (we get alot of posts where the OP hasn't even asked anything; just copied the question out of their book or whatever), it's ok.

As I understand it, s/he wants you to store the 4 digit number with cin. Then s/he wants you to split it into 4 separate digits. This would be very much easier with a loop and an array but you may not have studied loops and arrays yet. If you have, that simplifies things; if not, then that complicates things. If you've done loops and arrays you may well get more points but I don't want to tell you to do something that gets you marked down...

Anyway once you have your four digits you need to do this:
Replace each digit by performing the calculation (the sum of that digit plus 7) modulo 10.
digit1 = ((digit1 + 7) % 10);
which assigns the value of x + 7 % 10. For example, take x to be 3. x + 3 == 10. 10 % 10 == 0. So digit1 would be 0 if it had previously been 3. Does that make sense?

Then you need to
Swap the first digit with the third, and swap the second digit with the fourth.
1
2
digit1 = digit3;
digit2 = digit4;


Then you can do something like:
std::cout << "Encrypted digits: " << digit1 << " " << /* etc. */

You might consider how to write an application that will decrypt these values.]

I think he may be hinting at your next assignment. Think about how you got the results you got, and then how you'll get the original numbers back.

7 + 3 == 10, 10 - 3 == 7. Think about that.

Anyway hope that clears things up.
Use the programming techniques you used to solve Exercise 3.17 to insert statements that extract the digits from int variable number.


Try that.
If he can use an array (or another container) and a loop that will make this alot easier. Otherwise he's going to have to find some other way of taking a byte at a time out of 4 bytes.

If the user enters 1234 and he wants to take the one out, he's going to have to use a bunch of temporary variables.
If he used an array he could just do something like
1
2
    digit1 = myArray[0];
    digit2 = myArray[1]; // etc... 
yes, we have not worked on loops yet,(that is coming up) thanks for the tips. I'm going to see if I can go get this done!!
yep, I finished and got it correct. Thanks guys!!
Topic archived. No new replies allowed.