nthnwddll
nthnwddll

Reputation: 3

Caesar cipher input text file

I am trying to create a Caesar cipher using C++. I have the program read in a text file but I need it to encrypt the text and output to the screen.

This is my encryption code but I can't seem to get it to work. I have only just started using C++ and not really sure where to go from here.

cout << "enter a value between 1-26 to encrypt the text: ";
cin >> shift;

while ((shift <1) || (shift >26)) {
    cout << "Enter a value between 1 and 26!: ";
    cin >> shift;
}

int size = strlen(text);
int i=0;

for(i=0; i<size; i++) {
    cipher[i] = (text[i]);
    if (islower(text[i])) {
        if (text[i] > 122) {
            cipher[i] = ( (int)(text[i] - 26) + shift);
        }
    } else if (isupper(text[i])) {
        if (text[i] > 90) {
            cipher[i] = ( (int)(text[i] - 26) + shift);
        }
    }
}

cipher[size] = '\0';
cout << cipher << endl;

Upvotes: 0

Views: 5261

Answers (3)

stefan
stefan

Reputation: 3759

reformatted, made compilable ad fixed algorithm (to what i think was tried to achieve)

#include <iostream>
using namespace std;

char text[] = {"This is my encryption code but I can't seem to get it to work. "
               "I have only just started using C++ and not really sure where "
               "to go from here."};
char cipher[sizeof(text)];

void main()
{
    int shift;
    do {
        cout << "enter a value between 1-26 to encrypt the text: ";
        cin >> shift;
    } while ((shift <1) || (shift >26));

    int size = strlen(text);
    int i=0;

    for(i=0; i<size; i++)
    {
        cipher[i] = text[i];
        if (islower(cipher[i])) {
            cipher[i] = (cipher[i]-'a'+shift)%26+'a';
        }
        else if (isupper(cipher[i])) {
            cipher[i] = (cipher[i]-'A'+shift)%26+'A';
        }
    }

    cipher[size] = '\0';
    cout << cipher << endl;
}

Upvotes: 0

Jeff B
Jeff B

Reputation: 30099

A few things:

  1. You are checking if the character islower and then checking if the ascii value is > 122. This will never be true. In the default locale (standard ascii), islower() will only be true if the ascii value is in the range [97, 122] (a-z). The same goes for isupper(). It only returns true for ascii values between 65 and 90, inclusive.
  2. You are already working with ascii values anyway, so islower() and isupper() may be redundant. Those are equivalent to doing bounds checking on the ranges, i.e. text[i] >= 97 && text[i] <= 122. They are useful shortcuts, but don't base your code around them if you can simplify.
  3. Your code never adds the caesar shift if the value is <= 90/122, so you will never shift anything.

Upvotes: 0

Andrei
Andrei

Reputation: 5005

First of all, your algorithm is wrong.

If we assume ASCII input then you need to encrypt the values that are between 32 (i.e. space) and 126 (i.e. tilde ~), inclusive. You do this by adding the key (a single number) to the value. If the result is greater than 126 (your highest available character) you need to wrap around and start counting from 32. This means 126 + 1 = 32, 126 + 2 = 33, etc. Look up "modulo".

I recommend you look-up the word "debugging". Generally, when you have an algorithm you write code that matches the algorithm as best you can. If the results are not the expected ones then you step line by line using the debugger until you find the line were your expected results and your code's result no longer match.

Upvotes: 1

Related Questions