Can't Figure Out Encode and Decode C++ Problem

I'm having some trouble figuring out a C++ problem in my book. This is the part I really don't understand: "Note that what you encode cannot be directly decoded, as there is not a one-to-one correspondence". The way I was going to try to do this was by assigning each character a value and checking that to output the code. I'm completely confused as to how to solve this.

Thanks for the help.

Here it is:

Background Info:

Introduction to: Strings and Dates
At some point virtually all computer programs have to come in contact with
human beings with whom they have to communicate to interact. And a primary vehicle for this is textual information. We know that computers use combinations of binary digits to represent letters, numbers, and other symbols [e.g., punctuation]. The coding system most commonly found today is called ASCII [American Standard Code for Information Interchange].

It turns out that when it has to deal with strings of characters C++ treats them as an array [an array with the base type of char.] Dates are a specialized representation of data, and are treated in a couple of ways.


Assignment:

I came across a puzzle in a magazine that required you to decode a message composed of Wingdings icons. So, using what you’ve learned so far about C++ and the image below, write a C++ program that takes a text string [this text can include only alphabetic and characters] as input from a user, and using the relationships between letters and numbers present on a rotary telephone dial, encode the message. For example, the text string ‘hello’ would become ‘43556’. You then output back to the user the encoded message. Note that what you encode cannot be directly decoded, as there is not a one-to-one correspondence. What the puzzle makes you do is look at the frequency of certain symbols and go through various permutations that form words
that might be valid translations.


and here is the image:
[IMG]http://i.imgur.com/fHu6d.png[/IMG]
Last edited on
There isn't really anything to solve, necessarily. You have a collection of data that gets operated on by an algorithm. The data is organized by an index, in this case the then number on the telephone. Each number has with it an associated string, or list of characters. here is the algorithm:

1
2
3
4
5
6
7
Get the input from the user
for each element in the input, 
    for each element in the array
        if the input element is equal to the array element
               append the value of the index to the encoded string

output the encoded string
Last edited on
That's what I thought, but this threw me off:
"Note that what you encode cannot be directly decoded, as there is not a one-to-one correspondence"

Are you sure that's right?

Thanks
usually an encoded message relies on some sort of letter to letter mutation. For example, the letter 'b' might be replaced with the letter 'a'. By knowing what the offset is, you can write a program that will decode the message. In this case the translation would be a character by character shift of the alphabet. This is a simple cypher, and is called a Caesar Shift.

xtllx xtllx xtllx h fns knud hm lx stllx

Since the type of encoding that you want to reproduce does not rely on a shifting of the alphabet, a mathematical interpretation is somewhat complex. The solution set for each element is three. the number 2 can either be 'a' or 'b' or 'c'. Since 1 can be any of the three letters, its difficult for the program to come to a reasonable outcome. This is where the human needs to interact with the result to know what the meaningful output is. The sentence "I like Brownies" means nothing to the computer. Its a string of numeric representations in memory:

73 108 105 107 101 66 114 111 110 105 101 115

These numbers have no meaning to the computer either. These numbers are representations of this:

1001001 1101100 1101001 1101011 1100101 1000010 1110010 1101111 1101110 1101001 1100101 1110011

Since it means nothing, the program cannot look at the sentence and expound "that's the answer" because it doesn't know what a brownie is. Only you do.

This is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
	int i;
        double original;
	double code;
	cout << "Enter Text: ";
	cin >> string;

String^ charsAndSpaces = "";
for (int i = 0; i < chars->Length; i++)
	charsAndSpaces += chars[i] + " ";

cout << "Encoded Message: " << code << ".\n";
return 0;
}


I know it's not anywhere near done, but I'm not sure where to go from here. I understand what I have to do now, but not how to do it in C++.
Last edited on
the easiest way might be to check the value of each character against the ASCII code. If it lies within a certain range, assign it the number that corresponds to the letter.

For example:

1
2
3
4
5
6
7
8
#include <string>
using namespace std;

//...
string EncodedMessage;

if(character >= 97 || character <= 99) // 'a'
 EncodedMessage += '1';


look here for values:
http://www.asciitable.com/
I have to use the values on the phone in the image I posted, not ASCII. It's a rotary phone and it's not the same as a regular cell phone keyboard, but it's close. So for an example L would be 555 and J would be 5.

Thanks for the help.
Yes, but when the user enters the string as the programmer you need to be able to determine what they entered. One of the ways you can do this is by using the ASCII values. So if the user inputted 'a' you need to know that, and you can check if they did by using the ASCII code for 'a', which is 97.

Does this make sense?


(nice. This is post 404. Can you see this post? Otherwise you'll need to contact the system administrator. lol)
Last edited on
Thanks, I can see the post.

So I get an input, convert it to ASCII, and then display the ASCII in a message?

Didn't make sense at first, but I guess it does now, otherwise it would get confusing with a J and an L next to each other for example.

So can you show me how to combine what you posted above with what I already wrote? or will that not work?

Thanks again for all the help. Been stuck on this problem for a while now.
Well, what you are doing is using the equivalent ASCII value to determine what the encoded character is. So for J and L the ASCII range would be 74 through 76. If the character is within this range, you add to the the encoded string a '5'.

The computer doesn't know what 'J' is, but it knows 74.

Or you could do this:

1
2
3
4
if(char == 'j' || char == 'k' || char == 'l')
  EncodedString += '5';

//do the same for ever phone digit. 



Its essentially the same thing. anyway...

I'm not sure if this is homework, but its better and easier to use the string class. to do so you'll need to include <string>.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
using namespace std;

#include "stdafx.h"
#include <iostream>
using namespace std;

int main ()
{
        string Message;
        string EncodedMessage;
	cout << "Enter Text (without spaces): ";
	cin >> Message;

for (int i = 0; i < Message.Length(); i++)
{
     //here is where you want to check each position of the 
     //string to see where it is in relation of the phone.
     // you do this by using the [] operator of the string class.
}

cout << "Encoded Message: " << EncodedMessage<< ".\n";
return 0;
}



Last edited on
I have to use the values on the phone in the image I posted, not ASCII. It's a rotary phone and it's not the same as a regular cell phone keyboard, but it's close. So for an example L would be 555 and J would be 5.


Based on the text of the assignment, L, J and K would each be 5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>

struct letterMapping
{
    char letter ;
    char rep ;
};

letterMapping letterMap[] =
{
    {'a', '2' }, {'b', '2 '}, {'c', '2' },
    {'d', '3' }, {'e', '3' }, {'f', '3' },
    {'g', '4' }, {'h', '4' }, {'i', '4' },
    {'j', '5' }, {'k', '5' }, {'l', '5' },
    {'m', '6' }, {'n', '6' }, {'o', '6' },
    {'p', '7' }, {'r', '7' }, {'s', '7' },
    {'t', '8' }, {'u', '8' }, {'v', '8' },
    {'w', '9' }, {'x', '9' }, {'y', '9' },
};

const unsigned mapSize = 
    sizeof(letterMap) / sizeof(letterMap[0]) ;


int main()
{
    // for each letter in the message to be encoded,
    // find the letter in letterMap and use the associated
    // rep as the encoding - note 'z' and 'q' don't have
    // representations.
}



I see. Then all he has to do is loop through each element to determine it corresponding representation.
Thanks for posting, but I haven't learned to use structures yet.

I'm going to search for them online and if I can figure them out and code the for loops.

Thanks again
Can't figure it out and get it working. I'm going to try pogrady's way instead.
Thanks for posting though.
Do I have to change char to a new variable I made? Won't it think 'char' is a data type instead of a variable?
1
2
if(char == 'j' || char == 'k' || char == 'l')
  EncodedString += '5';
Last edited on
?
Topic archived. No new replies allowed.