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.

## All 39 Replies

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.

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 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...";
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...";
return 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.

commented: exactly :) +34

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

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?

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.

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.

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.

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

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.

Oh, believe me I will...

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

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.

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.

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 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...";
exit(1);
}

// Close the input file stream
inputFile.close();
cout << "Press enter to continue...";
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, 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.

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?

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?

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.

OK, here's my another code. inputFile in inputFile.close is red, after putting it in void, as well as getline and reply. I just need to pass this class, nothing else. And with my present grades it's possible, but only if I have A's and B's. It's strongly not recommended that I withdraw it, though if I fail this homework and quiz, then I'll have to.

``````// 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 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...";
exit(1);
}

// Close the input file stream

}

void originalLetters() {
char c = 'A';
if( isalpha(c) ) {
c -= 3;
if( c < 'A') {
c = 'Z' - ('A' - c)+1;
}
}
cout << c << '\n';
inputFile.close();
cout << "Press enter to continue...";
return 0;
}``````

So, anyone out there? Will you help me?By the way, if you really want to know, I don't have paypal.

Looks like I should add strange function, huh?

My suggestion is to start over. Write only the first part of the code and get it to compile and run without errors:
1) Read a line of text
2) Write out that same line of text.

Nothing more until it work perfectly. Then move on to the next step.

Looks like I should add strange function, huh? OK, so I counted my grades, and so if I don't get A and B for these 2 last homeworks, I'm doomed. Of course there will be final, but let's leave it until the end of the term.

What step, WaltP? What about void function? OK, so, let's try and I create the loop. But what would be my next step? And let me tell you the book I'm using: it's C++ Programming. From problem analysis to program design. By D.S. Malik. 5ed. Chapter 7 is what we're studying right now, one chapter per week. So, I either have to use void (which looks right to me) or enumeration, from the next chapter. So, which one? So, let's even suppose I'll use enumeration. But I don't even know how to use it properly yet, it'll be in the next chapter, next week.

All right, here's teacher's code edited by me. Not sure yet how to use it yet, but I'll try to get it asap. Help is welcome, though.

``````// Written by: Mikhail Popov
// Sources: None
// Date: Nov 5, 2010

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

using namespace std;

int main ()
{
bool finished = false;
int selection;
char letter;                  // Note that letter is of type 'char'.
int asciiCode;                // Note that asciiCode is of type 'int'. // Declarations
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...";
exit(1);
while (! finished) {
cout << "What do you want to do?" << endl;
cout << "1. Convert ASCII to Decimal?" << endl;
cout << "2. Convert Decimal to ASCII?" << endl;
cout << "3. Quit?" << endl;
cout << "Enter 1, 2 or 3: ";
cin  >> selection;
switch (selection) {
case 1:
cout << "Please enter a letter: ";
cin >> letter;
// Note the use of the <static_cast> to convert char to int.
asciiCode = static_cast<int>(letter);
cout << "The ASCII code for the letter " << letter << " is " << asciiCode << endl;
break;
case 2:
cout << "Please enter a number: ";
cin >> asciiCode;
if (asciiCode < 0 || asciiCode > 127) {
cout << "The number must be between 0 and 127" << endl;
} else {
// Note the use of the <static_cast> to convert int to char.
letter = static_cast<char>(asciiCode);
cout << "The letter represented by decimal " << asciiCode << " is " << letter << endl;
}
break;
case 3:
finished = true;
break;
}
}
cout << c << '\n';
inputFile.close();
cout << "Press enter to continue...";
return 0;
}``````

P.S. Anyway, since we're studying void and will be studying enumeration, I think we should consider those

The next step is whatever you have to do once you are able to read the string and can write it out. Whatever that is. Start with what I mentioned above with a new project and build it a piece at a time.

Looks like I should add strange function, huh? OK, so I counted my grades, and so if I don't get A and B for these 2 last homeworks, I'm doomed. Of course there will be final, but let's leave it until the end of the term.

Hey, no one cares. Your fault.

OK. Then the first step should be the easiest one. Right? So, if it is, let's open a file. Next step would be to use that function c=a whatever, do I get it right? Or it should be using enumeration?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.