954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Caesar Cipher Code

Hello. So, I'm a newbie, and this is my second hard assignment. In this assignment, I have to decrypt the message from the teacher (well, not only this, it should open and decrypt any text file). So, the example line would be:
Ftq Pqoxmdmfuaz ar Uzpqbqzpqzoq ar ftq Ftudfqqz Oaxazuqe
Uz OAZSDQEE, Vgxk 4, 1776

But what I'm most concerned about, is that
Rad uybaeuzs Fmjqe az ge iuftagf agd Oazeqzf: should mean something I don't know what, since in the cipher, "Return to Rome" would be encrypted as, "Uhwxuq wr Urph". 3 letters ahead. So, Rad should mean what, O(w?)a. I mean, WHAT??? So, I reviewed some examples here, none of which work the right way. Please help me. It's due this Sunday, 10 o'clock.

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

Before attempting to decrypt that you need to know the shift value, and whether it was shifted right or left. I suppose you could solve it by iterating through all 26 possible shift values ('A' - 'Z' = 1-26) assuming case is ignored.If not then there would be at least 127 possible values, which includes every key that can be typed on the American keyboard (except special keys). First try a shift of 1 Right, look at the results to see if it makes sense. If not, then use 2R and repeat the process.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

It's 3 letters forward shift, I said. And it should be only letters. No numbers should be shifted. This code should be helpful, since it read every each character at a time. But how do I move it 3 shifts back to read original message?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    // Declarations
    string reply;
    string inputFileName;
    ifstream inputFile;
    char character;

    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());      // Need .c_str() to convert a C++ string to a C-style string
    // Check the file opened successfully.
    if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }

    // This section reads and echo's the file one character (byte) at a time.
    while (inputFile.peek() != EOF) {
        inputFile.get(character);
        cout << character;
    }
    cout << "\nEnd of file reached\n" << endl;

    // Close the input file stream
    inputFile.close();
    cout << "Press enter to continue...";
    getline(cin, reply);
    return 0;    
}
miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

The ASCII table shows that A - Z has ASCII code of 65 - 90. On the other hand a - z has ASCII code of 97 - 122

You could manipulate the text as 'char' using ASCII code. For example, in the Caesar cipher, B (66) would be E (69) after encryption. Its just as simple as adding 3 to the ASCII code and you'll get the correct 'char'. To decrypt, simply minus 3 from the ASCII code and you'll get the decrypted value.
NOTE: do remember to wrap around. E.g. Z comes after A when decrypting

Hope this shed some light to you.

chiwawa10
Junior Poster
156 posts since Jul 2005
Reputation Points: 88
Solved Threads: 27
 

chiwawa10, that is a nice idea! can I get the code for this?

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

So, anyone? How would I shift those letters 3 spaces back? Also, I have to use void function, since we're studying it now. Maybe some other predefined functions. Is that code useful in any way?

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

Well, all you have to do is subtract 3 from the character

int main()
{
    char c = 'A';
    if( isalpha(c) )
    {
        c -= 3;
        if( c < 'A')
        {
            c = 'Z' - ('A' - c)+1;
        }
    }
    cout << c << '\n';
        
}


Now put that in a loop and do it for each character in which isalpha() is true. You will also have to modify that program to recognize lower-case characters.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

for each character? that way the code will be too long, and I for some reason don't see a void or any other user defined function... IT HAS TO BE IN HERE! it's all my homework is about.

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 
for each character? that way the code will be too long, and I for some reason don't see a void or any other user defined function... IT HAS TO BE IN HERE! it's all my homework is about.


So you were expecting us to write your program for you? Not gonna happen. You're taking the class, you know what a void function is, you program what you can. We can help you correct it, but you still have to write it.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Also: using CAPS and BOLDTAGS with EXCLAMTIONMARKS!! is probably not going to motivate the people here to help you.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

I didn't mean for you to just blindly copy the code snippet I posted 52 times for each character, but use it as an example of how to do it in a loop. I showed you how to do it for one character, now you should be able to expand on that and do it for all the characters is the string. You can put the code in another function if you want to, but that's not really needed.

I think the whole point is to teach you how to solve problems. You won't learn if we do all the work for you.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Oh, believe me I will...

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

So, someone? OK, look. If you want me to write the code, I could just stupidly copy it from one of the guys' postings, and edit it just any way I want. Do you really need this? So, if you write me the code, there will be more chance for me to learn

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

First, if you don't know how to construct code to solve a problem, how do you know that blindly modifying would make the code work the way it should be? Second, if someone post a working code, it is a working code! So why do you need to modify it anyway? Is this the way you do your homework? By getting someone else solution and change variable names? If you don't show us you understand but don't know how to solve the problem (apply an algorithm), we would be gladly to help you. If you just come here and ask us to deliver the code for you, I doubt you would get it that way.

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

Looks like we have another one, guys...

OK, here's the deal. This is not a forum where you request a program and we jump to it and write code for you. If you want that, many of us can be hired to code it for you. At the going rate.

And using the BS that if we write it for you give you a better chance to learn is not going to help. That's such a pathetic idea. It's also called cheating.

Write some code and we'll help you fix it. Free. Otherwise, many of us have access to PayPal.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

The pathetic idea, if you want to know, is that this forum is in fact created. What is it for? Helping. Right? So, if it is not for helping, sorry. But what else have I forgotten here?

But OK. Here's deal. Since this thread has two codes, one is teacher's that I gave and one is someone else's, let's do it like this. How about if I edit teacher's code with a void function that will contain that other code? Will it work? I mean, of course it should work, but what should I write?

Or wait, maybe I should also use global variable? So, here's a rough draft:

// Written by: Mikhail Popov
// Sources: None
// Date: 3-Aug-2007

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void originalLetters();

int main ()
{
    // Declarations
    string reply;
    string inputFileName;
    ifstream inputFile;
    char character;

    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());      // Need .c_str() to convert a C++ string to a C-style string
    // Check the file opened successfully.
    if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }

    // Close the input file stream
    inputFile.close();
    cout << "Press enter to continue...";
    getline(cin, reply);
    return 0;	
}

void originalLetters() {
	char c = 'A';
	if( isalpha(c) ) {
		c -= 3;
		if( c < 'A') {		
			c = 'Z' - ('A' - c)+1;
		}
	}
		cout << c << '\n';
}
    cout << "\nEnd of file reached\n" << endl;

It doesn't work, though. Lots of mistakes. Don't know why.
P.S. OK, it works. But wrong way! It won't show anything

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

miha2, there's always no harm in trying. As you can see, there are lots of experts in this forum who are helpful and friendly. Like all other threads, just work out the initial code on your own and post any compilation error or problem that you face. All are more than willing to help.

chiwawa10
Junior Poster
156 posts since Jul 2005
Reputation Points: 88
Solved Threads: 27
 

It seems like you aren't able to think on your own, and expect to be spoon-fed.

Do you even know what a function is, and how to use one?
So, you did put the code inside a function. And you don't know what to do next.. I suggest you read up on functions.
Since the solution to the only "hard" part of the assignment was already given to you.
Why do you even take a programming course?

Kremlan
Newbie Poster
7 posts since Nov 2010
Reputation Points: 7
Solved Threads: 0
 

Yes, I do take programmng course, but it's online. That's why I'm asking.
I am able to think on my own, I only have problems with algorithms. A function is an int and what it follows, for example. So, int main() { } is a function. Like I said, it should have void function and (most likely) glopal variable, since it's all this chapter is about. And let's make another deal. OK, I know very little about programming, and would like to know more, but just please help me with these 2 last homeworks, and I'll never ask you of anything else in C++ programming field. What do you say?

miha2
Light Poster
26 posts since Oct 2010
Reputation Points: 0
Solved Threads: 0
 

The code you posted a couple hours ago --

main() opens a file then immediately closes it. What is the purpose of that? If you are not going to do anything with the file then why bother opening it?

line 51 appears to be outside a function. Move it up before line 50.

>>But wrong way! It won't show anything
Because you never told it to show something. originalLetters() function is never called. Do you own a TV set? If yes, then you know that you have to turn it on before it will show you something. Same in computer program. originalLetters() exists but won't do anything if its never called.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You