Need help :(

Hi, I just started learning c++ not so long ago.
I asked a friend to give me some c++ exercises so i can try and do them, i did most of them but i got stuck on one and i can't seem to solve it.
I would really appreciate it if anyone can help me with it, so i can see how it's done and learn to do it.


* Encoding and decoding of text and use of
different key as the variable assigned by the user 39.

-To compile a coding and decoding program consisting of text only
uppercase letters and spaces.
Use a menu with two functions to choose from, encoding and decoding.
When testing a task using a test sentence,
which will first be encrypted and then decrypted.
The text should be encoded so that each letter will be
replace with a character whose ASCII code has been increased by
the smallest digit of an arbitrary three-digit number entered,
(for example when entering number 231 all letters will consequently be moved to
one position ahead of the original.
The code should be found for each number).
Each time the test sentence and the three digit number are
different and be entered as variables.
When deciphering understandably, the same three-digit number is used as the key.
Last edited on
You start with something simple to build your knowledge.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

char encode(char c, int key) {
  return c;
}
char decode(char c, int key) {
  return c;
}
int main( ) {
  char c = 'A';
  int key = 0;
  char e = encode(c,key);
  cout << e << endl;
  char d = decode(e,key);
  cout << d << endl;
}
The hard part is figuring out what to do if the encoding/decoding causes the letters to "wrap around" For example, if the shift value (the last digit of the 3-digit number) is 2, then Y and Z encode to A and B. You can handle this with:
 
if (encodedChar > 'Z') encodedChar -= 26;


When decoding, you worry if the decoded characters has fallen below 'A':
if (decodedChar < 'A') decodedChar += 26;

Get SalemC's program to encode and decode correctly. Then modify it to encode/decode entire strings. Then modify it again to add the user interface.
Thank you guys very much for the help.
Topic archived. No new replies allowed.