Hello everyone,

I am having trouble with an assignment. The assignment consists of a basic encryption and decryption program already written for me, I just have to write the encryption function. What we have to get the program to do is enter an integer and a text, and get the program to increment each letter in the text by the integer given. I did this by using a for loop and incrementing each value in the string by the integer.

However, I still can't get it to decrypt and I need the program to work with only a-z letters (if I increment each letter by 3 and I have the letter Z, it should go to Z+3 = C).

I attached the description of the attachment and below are the codes: The first file does not need to be edited.

Thanks for helping :)

Link to image of assignment: http://i41.tinypic.com/v4ta3s.jpg

/*******************Programming Assignment 1***************/
/****************Caesar's substitution cipher**************/

/*****************YOU MUST NOT EDIT THIS FILE**************/

/****Substitute alphabets in a string using rotation key***/
/****Leave all other characters unchanged******************/
/****Confirm correct encryption by decrypting**************/
/****to recover original text******************************/

#include "encrypt.h"
#include "studentID.h"

using namespace std;

int main () {
        // define the rotation index variable and
        //the new rotation index variable to recover the text
        int rot, newrot;

        // define the input string (input), encrypted string (output)
        // and the recovered string (decrypted)
        string input, output, decrypted;

        // top level loop to perform encryptions
        while (1) {
                std::cout << "Please enter rotation key (or -1 to exit)" << endl;
                // read in the rotation index from user
                std::cin >> rot;

                // if the user is done (rot == -1), then exit
                if (rot == -1) break;

                // read in the text to be encrypted (terminated by new line)
                // use "ignore" method of stream cin to get rid of
                // accumulated new line characters
                std::cout << "Please enter text to be encrypted" << endl;
                getline(std::cin, input);

                // encrypt the input text
                output = encrypt(input, rot);
                // print out the encrypted text
                cout << endl << "The encrypted text is " << endl << output << endl;

                // calculate a new rotation value to recover original text
                // from the encrypted text -- used for testing only
                newrot = 26 - (rot%26);
                // recover original text by calling encrypt with new rotation value
                decrypted = encrypt(output, newrot);

                // print out the recovered text -- should match original
                cout << endl << "The recovered text is " << endl << decrypted << endl << endl;
        } // end while loop
} // end main function

This part needs to be edited:

/*****************YOU MUST EDIT THIS FILE******************/
/************Complete function encrypt below***************/

#include "encrypt.h"
using namespace std;

string encrypt (string input, int rot) {
    for (string::size_type i = 0; i < input.length(); ++i)
    return input;
} //end function encrypt
3 Years
Discussion Span
Last Post by ddanbe

Well the problem you are having is you are not doing what the assignment asks. When you add to the letter if you exceed the bounds of the character set you need to wrap around to the beginning. You also don't want to modify anything that isn't a character. All you are doing is adding the rotation value to the letter. As a hint I'll give you a way to add to a letter and if past 'z' or 'Z' it will start over at 'a' or 'A' respectivly.

int rot = 4;
char ch = 'W';

if (ch >= 'a' && ch <= 'z')
    ch = (((ch + rot) < 'z') ? ch + rot : 'a' + ((ch + rot) - 'z' - 1));
    ch = (((ch + rot) < 'Z') ? ch + rot : 'A' + ((ch + rot) - 'Z' - 1));

you must convert each character of string to a character type variable and the using type casting you can do it.

char ch = 'A';
int en = ch;     //en == 65;
en = en + 3;
char = en;       // char == D;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.