I'm trying to write a code for class that basically takes a 4 digit input, scrambles the the places of each number so that the first digit is in the third digits place, and add 7 to each number. This is supposed to be simple encryption. I know my functions are wrong, but I threw this together for something to show, and I have no idea what I may be forgetting/doing wrong. If anyone could look this over and give me some tips, I'd much appreciate it. Thank you for taking the time to read this.

#include <iostream>

using namespace std;



int main()

{

    int number, dig1, dig2, dig3, dig4, encryptnum;



    cout << " Enter a four digit number to encrypt ";

    cin >> number;

    

    number%10 = dig1;

    number = number/10;

    number%10 = dig2;

    number = number/10;

    number%10 = dig3;

    number = number/10;

    number%10 = dig4;

    encryptnum = (dig3 + 7)(dig4 + 7)(dig1 + 7)(dig2 + 7);



    cout << " Encrypted Number "<< encryptnum <<end1;



    return 0;

}

yes there r some clear cut errors in writing codes do replace as

dig1=number%10 in place where number%10=dig1; n others

and put * between parenthesis as for multiplication...

>> number%10 = dig1;

That is backwards. dig1 = number % 10;

As visitor11 said you need to put the variable to be assigned on the left hand side of the equals sign otherwise it will try to assign number%10 and it cannot do that.

C++ uses operators to do multiplication division addition subtraction and it does not use brackets for multiplication. You can probably overload the bracket operator to do multiplication but that just creates more work when '*' already does the job.

One thing I noticed with your encryptnum is that if any of the digits are 3 or more then its gonna carry the tens digit over to the next number and I'm not sure if you want that. If you do just leave what I am about to post and if you do not use the mod operator '%' (ie dig3 = (dig3 + 7) % 10) to give you a 1 digit number. encryptnum = (dig3 + 7) * (int)pow(10.0, 3.0) + (dig4 + 7) * (int)pow(10.0, 2.0) + (dig1 + 7) * (int)pow(10.0, 1.0) + (dig2 + 7); You will need to include the math header ---> #include <cmath>
for the pow(double, double) function.

Also you are using end1 <--- with the number one
when it is supposed to be endl <--- with the letter 'L' for line

Thanks for the help. I should have clarified, I'm not trying to multiply the digits, but combine them back into one number ordered as follows: dig3dig4dig1dig2. How would I accomplish this?

encryptnum = (dig3 * 1000) + (dig4 * 100) + (dig1 * 10)+(dig2) + 7777 ;

I was wrong! :)

No. That will not work. If you are not sure about something then try it out on your own compiler before posting here.

Running into a problem with my code. In the example, it says as an example, the input of 1009 should output 7687. Instead, I am getting the output 7967. What am I doing wrong?

#include <iostream>

using namespace std;



int main()

{

    int number, dig1, dig2, dig3, dig4, encryptnum;



    cout << " Enter a four digit number to encrypt ";

    cin >> number;

    

    dig1 = number % 10;

    number = number/10;

    dig2 = number % 10;

    number = number/10;

    dig3 = number % 10;

    number = number/10;

    dig4 = number % 10;

    encryptnum = (dig3 * 1000) + (dig4 * 100) + (dig1 * 10) + (dig2 * 1) + 7777;



    cout << " Encrypted Number "<< encryptnum <<endl;



    return 0;

}

7687 can't be right because if you try to unencrypt it one of the digits will be a negative number

7 6 8 7
subtract 7 from each digit
0 -1 1 0


If you try to add 7777 to number before doing anything, then the result is 7868.

Through looking at it more, it seems that if you have a number like -1, it really means 9. This code can't allow double digit numbers, so thats its way of resetting it. How do I make this work? So 7687 would decrypt to be 1009 (remember that the numbers need to be reordered from 3412 to 1234).

Any ideas?

I threw a for loop in and a function but if you aren't allowed to use functions you can use the same method in the 1st few posts.

This adds 7 to each digit then gets the remainder of each new digit by using mod 10 and then puts them in the 3412 order.

#include <iostream>
using namespace std;

int E(int exp) //function returns 10^exp
{
    if( exp <= 0 )
        return 1;
    int r = 1;
    for( int i = 0; i < exp; i++ )
        r *= 10;
    return r;
}

int main()
{
    int num;
    int digits[4];
    cout << "Enter a 4 digit number to be encrypted: ";
    cin >> num;

    for( int i = 0; i < 4; i++ )
        digits[3-i] = (((num%E(i+1))/E(i))+7)%10; //takes the digit from the input adds 7 and "mods" it to a 1 digit number

    int temp[2] = {digits[0], digits[1]}; //stores these 2 digits because they get overwritten when changing the order
    digits[0] = digits[2];
    digits[2] = temp[0];
    digits[1] = digits[3];
    digits[3] = temp[1];

    for( int i = 0; i < 4; i++ )
        cout << digits[i] << endl;

    return 0;
}

We can't use those yet. If I wanted to make it work off of what I already had, what would I need to do to make it not give negative numbers? Instead of giving say 6 - 7 = -1, have it give 9 and the output.

This should do what you want need

int x = 6-7;
if( x < 0 )
    x += 10;

Sorry, but I just confused myself even more. I dont need to worry about decryption really, just encryption. How would it to work so that when it adds the 7 to the number, that it doesnt go over 10? For example 7+7 would output 4 instead of 14. How would I get that to work with all the digits and numbers from 3-9? all of those result in numbers greater than 10, which throw off the encryption. I would need to subtract 10 from those at that point to get it to fit the code. Does this even make sense to anyone anymore because I feel lost.

If you look at what I posted before I used %10.

for example

int x = 7+7;
x %= 10;
cout << x << endl; //this would output 4

% or mod or Modular is the remainder of the division.

for example 14/4 has a remainder of 2 but this will equal 3.
14%4 will equal 2 because that is the remainder.

edit: had wrong variable name so cout << num << endl; would not actually give you 4 (haha)

Would I have to add the int x = 7 + 7 variable for all numbers 3-7? or is there a way I can group the numbers 3-7 to fit that condition?

OK if you went (2+2)%10 that would equal 4 because 4/10 has a remainder of 4
(7+7)%10 would give you 4 because it also has a remainder of 4

You do not know if you are going to have any numbers that are greater than 3 or any numbers below 3 and since it will not give you a wrong value by using %10 on all the digits it wouldn't hurt to just do it instead of making a big logic block trying to sort out if it is greater than or less than 3.

If you look back at the code I posted on line 22 that is where all the digits get split and have 7 added to them then they I used %10 on them to make them one digit. The code was posted so that you can look at it and see what is going on then if you want to put it back into your code you can see the logic in what had been done and understand how to implement it into your code.

Ah I see now. Thanks for helping me out. I've been really stubborn with this and just having problems with getting the hang of it. Thanks for dealing with me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.