Help with entry level Encryption program

hello, I was given a task to write a program to encrypt the first character of a word. The word will be entered by the user. Additionally, the user will enter a string value to be utilized as a translation map when determining how to translate the character. default map: "zyxwvutsrqponmlkjihgfedcba"

the sample output i should be getting:
$g++ encrypt.cpp
$runa.out What is the translation map(type 'default' to use default):default
What is the single word to translate: hello
Encrypted word: sello
$
$run a.out Whatisthetranslationmap(type'default'tousedefault):fghjkl
Error:invalid translation map size. $


Last edited on
Well, on line 15 you check to see if the user entered exactly 26 characters for the new translation map. If he does not, line 17 complains to the user.

(Rather than complain to the user, what can the program do to 'fix' the user's input to be a 26-character mapping?)

BTW, that there code you posted is not the code that produced your example session.

Hope this helps.
guyinNeed wrote:
thank you for commenting on my post, however, i am still a little confused about how to solve the problem. is your recommendation going towards
if ( userInput.at(0) >= 'a' && userInput.at(0) <='z')) ??

No...

The map is a 1-to-1 lookup table. Normally the alphabet is ordered like this:

    "abcdefghijklmnopqrstuvwxyz"

If you were to use that as your lookup table, you'd get for output the same thing you gave as input, because 'a' --> 'a', 'b' --> 'b', etc.

The default lookup table is:

    "zyxwvutsrqpomnlkjihgfedcba"

If you put them together you see the relationship:

abcdefghijklmnopqrstuvwxyz
zyxwvutsrqpomnlkjihgfedcba

That is, 'a' --> 'z', 'b' --> 'y', etc.

Now, assume the user enters, for input, "fghjkl".

abcdefghijklmnopqrstuvwxyz
fghjkl

Notice something missing?
('a' --> 'f', 'b' --> 'g', etc, but what does 'g' --> ?)

You need to either:

(1) Require the user to enter 26 letters exactly (no less), or
(2) Automatically fill-in the missing letters.

For method two, consider the consequence of using the letters "fghjkl":

  abcdefghijklmnopqrstuvwxyz  <-- letters available
-      fgh jkl                <-- letters used
----------------------------
  abcde   i   mnopqrstuvwxyz  <-- letters not used

Having removed the used letters, we can stick the unused ones at the end of the user's input.

    "fghjkl" + "abcde" + "i" + "mnopqrstuvwxyz" --> "fghjklabcdeimnopqrstuvwxyz"

Now there is a proper mapping:

abcdefghijklmnopqrstuvwxyz
fghjklabcdeimnopqrstuvwxyz


Hope this helps.
deleted
Last edited on
I don't think your TA was paying any attention to you at all.

It looks like the program that the teacher is using is submitting a translation map that is not 26 characters long.

(Notice that it shows your error message from line 36.)

So it looks like you may have to do option 2 above.


You could also just try submitting a program that simply prints what it gets, to see what the teacher's program is trying to use:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string mapping;
  cin >> mapping;
  cout << "The user mapping is \"" << mapping << "\"\n";
  cout << "It is " << mapping.length() << " characters long.\n";
  return 0;
}

Sorry.
Topic archived. No new replies allowed.