i don't know what i'm doing wrong in this program. i'm supposed to write a program that would encrypt and decrypts four-digit integers.it should encrypt it as follow: replace each digit by the remainder after the sum of that digit plus 7 is divided by 10. then swap the first digit with the third and the second digit with the fourth and then print the encrypted integer. same for decrypt. here is what i have:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>


int main(void)
{
	char encrypt[] = "e";
	char decrypt[] = "d";
	//int x;
	int num1;
	int num2;
	int num3;
	int num4;
	int anyNum1, anyNum2, anyNum3, anyNum4;

	
	printf("Encrypt or Decrypt (e/d)\n");
	scanf("%s", &encrypt);
while (num1 != 0, num2 != 0, num3 != 0, num4 != 0)
{
	printf("Enter number\n");
	scanf("%4d", &num1, &num2, &num3, &num4);

    anyNum1 = printf("%d" + 7 / 10);
	anyNum2 = printf("%d" + 7 / 10);
	anyNum3 = printf("%d" + 7 / 10);
	anyNum4 = printf("%d" + 7 / 10);


	printf("1 2 3 4" " encrypted = %4d ", anyNum1, anyNum2, anyNum3, anyNum4);
}
//else
//{
  //  printf("Enter number\n");
 //   scanf("%4d", &num1, &num2, &num3, &num4);
  //  anyNum1 = (num1 * 10) - 7;
	//anyNum2 = (num2 * 10) - 7;
	//anyNum3 = (num3 * 10) - 7;
	//anyNum4 = (num4 * 10) - 7;

   // printf("1 2 3 4" " encrypted = %4d ", anyNum1, anyNum2, anyNum3, anyNum4);
//}


	return 0;
}

Lines 26 - 29:

Why would you want the return value of printf?

From http://www.cplusplus.com page on printf:

Return Value
On success, the total number of characters written is returned.
On failure, a negative number is returned.

Is that what you want? I seriously doubt it.

Lines 19 and 20 don't make any sense. You should be asking the user to enter a character and store it in a variable that should be named userOption or something, then compare it to 'e' or 'd' and act accordingly.

To clarify your algorithm, to encrypt 1234, you break it into digits:

1
2
3
4

Add 7:

8
9
10
11

Divide by 10 and take the remainder:

8
9
0
1

Swap 1st and 3rd, 2nd and 4th:

0
1
8
9

So 1234 encrypts to 0189, and 0189 decrypts back to 1234? Is that the idea?

I think you need to go back to the drawing board. Isolate each digit, do some mathematical operations on them, then recombine them, then print. You should be doing no mathematical manipulation in a printf statement.

thank you for your help....this is how it should appear as:
also i don't know how to take the remainder more like i don't know how to put it into a code.


Encrypt or Decrypt (e/d)
e
Enter number
1234
1234 encryped = 0189
Continue? (y/n)
y
Encrypt or Decrypt (e/d)
d
Enter number
0189
0189 decrypted = 1234
Continue? (y/n)
n

Edited 7 Years Ago by kele1: n/a

You're in the C++ forum, so presumably you can use cin and cout in lieu of scanf and printf if you choose. scanf and printf will work too, but most people find cin and cout easier.

You isolate digits with the / and % operators.

Here's the ones and hundreds digits. I'll leave the tens and thousands digits to you.

int someNumber, ones, hundreds;
someNumber = 4317;
ones = someNumber % 10;
hundreds = (someNumber / 100) % 10;

Regarding whether to decrypt or encrypt, you need to, one, decide whether you are using scanf or cin, and two, decide whether you are using C++ style strings or C-style strings or a simple char to store the user's input. From there, compare the user's input to 'e' and 'd' and see which it is.

Start from scratch, because there are too many problems with your last program. Also, are you using a C compiler or a C++ compiler?

You're in the C++ forum, so presumably you can use cin and cout in lieu of scanf and printf if you choose. scanf and printf will work too, but most people find cin and cout easier.

You isolate digits with the / and % operators.

Here's the ones and hundreds digits. I'll leave the tens and thousands digits to you.

int someNumber, ones, hundreds;
someNumber = 4317;
ones = someNumber % 10;
hundreds = (someNumber / 100) % 10;

Regarding whether to decrypt or encrypt, you need to, one, decide whether you are using scanf or cin, and two, decide whether you are using C++ style strings or C-style strings or a simple char to store the user's input. From there, compare the user's input to 'e' and 'd' and see which it is.

Start from scratch, because there are too many problems with your last program. Also, are you using a C compiler or a C++ compiler?

i'm using c++ compiler

i'm using c++ compiler

thnx everyone for your help i was able to figure it out

This article has been dead for over six months. Start a new discussion instead.