2 Replies - 221 Views - Last Post: 02 March 2018 - 08:45 AM Rate Topic: -----

#1 kyoklo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 18

encoding/decoding help

Posted 01 March 2018 - 11:00 PM

i am trying to write a encoding/decoding program but have gotten stuck when it comes to the program printing out my encoded message and i was wondering what i was doing wrong.

#include<stdio.h>
#include<stdlib.h>
#include <string>
#include <iostream>

using namespace std;


int main()
{
	int i, x;
	char str[100];
	char junk;

	printf( "Please enter your message: ");
	scanf_s("%c",& str);
	scanf_s("%c", &junk);
	printf("\nWhat would you like to do? :\n");
	printf("Encrypt your message = 1: \n");
	printf("Decrypt your message = 1: \n");
	scanf_s("%d", &x);

	
	switch (x)
	{
		
	case 1:
		for (i = 0; (i < 100 && str[i] != '\0'); i++)
			str[i] = str[i] + 2; 

		printf("\nYour Encrypted message is: %c\n\n\n", & str); 
		break;

		
	case 2:
		for (i = 0; (i < 100 && str[i] != '\0'); i++)
			str[i] = str[i] - 2; 

		printf("\nYour Decrypted message is: ", &str);
		break;

	default:
		cout << "\n Please try again, Invalid input\n";
	}
	system("pause");
	return 0;



Is This A Good Question/Topic? 0
  • +

Replies To: encoding/decoding help

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14088
  • View blog
  • Posts: 56,440
  • Joined: 12-June 08

Re: encoding/decoding help

Posted 02 March 2018 - 07:51 AM

Stuck in which way? Is there an error? If so please copy/paste the text here. I would also suggest telling folk what your input is, what your output is, and what the output should look like.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: encoding/decoding help

Posted 02 March 2018 - 08:45 AM

Hmm... your goal seems to be to shift the values by two in one direction or another. Is this perhaps a variant of the Caesar cipher?

Understand that 'A' + 2 will indeed equal 'C'. However, 'Z' + 2 will equal '\', see here: http://www.asciitable.com/

If you want 'Z' + 2 == 'B' and 'B' - 2 == 'Z', you're going to have to work for it. Rather than trying to encode the string, begin with a function that will simply encode a character.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1