Basic Caesar cipher problem

Hey guys,
I'm having a problem with my c++ programming assignment. It is supposed to 1) open cipher.txt and read it into a char array. 2) Use a "prepare" function to read the contents of raw.txt, remove all characters that are not alphabetic or spaces and write them to source.txt. 3) Read source.txt and encode the message using the cipher to encrypted.txt 4) use the function "inverseCipher" to generate the decoding substitution. 5) Decrypt encrypted.txt and write the solution to verify.txt and then compare verify.txt to source.txt. I don't have much of a problem with the cipher itself. My problem is with the reading and writing of the files. I guess I just don't have a good grasp of it. I've attached some code that I have been able to piece together too. I would normally be able to figure this out if I had enough time, but I've spent the day in the hospital with my girlfriend who has a very high fever. Even you cannot reply in time I'm still interested to hear how you guys think this problem should be solved.
Thanks,
Iqloo
//code.h
#ifndef _CODE_H
#define	_CODE_H

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

class Code
{
public:
    Code();
    Code(char[]);
    void prepare(ifstream&, ofstream&);
    void generateCode();
    char encode(char);
    char decode(char);
    void inverseCipher();

private:
    char cipher[26];
    char decipher[26];
};


#endif	/* _CODE_H */

//code.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include "code.h"

using namespace std;

Code::Code()
{
}

void Code::prepare(ifstream&, ofstream&)
{
    char ch;
    while((ch = inFile.get()) != EOF)
    {
        if(isalpha(ch)|| isspace(ch))
         ch = tolower(ch);
        else
            continue;
    }
}

char Code::encode(char)
{
    char ch, en, i;
    ch = inFile.get();
    i = static_cast <int> (ch);
    i -= 97;
    en = cipher;
}

char Code::decode(char)
{
    char ch, de, i;
    ch = inFile.get();
    i = static_cast <int> (ch);
    i +=97;
    de = decipher;
}

void Code::inverseCipher()
{
    int i, j;

    for(i = 0; i < 26; i++)
    {
        j = static_cast <int> (cipher[i])-97;
        decipher[j] = static_cast <char> (i +97);
    }
}

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cctype>
#include "code.h"


//main.cpp
int main(int argc, char** argv)
{
    ifstream inFile;
    inFile.open("cipher.txt", ios::in);
    if(!inFile)
    {
        cout << "Failed to open cipher.txt" << endl;
        exit(1);
    }

    char c[26];
    for(int i = 0; i < 26; i++)
        inFile >> c[i];

    Code myCode(c);
    inFile.close();

    inFile.open("raw.txt", ios::in);
    string raw;
    while(getline(inFile, raw))
    {
        myCode.prepare(inFile, outFile)

    }

    return (EXIT_SUCCESS);
}

Open in new window

iqlooAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>  My problem is with the reading and writing of the files. I guess I just don't have a good grasp of it.

Have a look at this tutorial in file I/O in C++ :

        http://www.cplusplus.com/doc/tutorial/files/


What you want to do, is that each function (prepare, encode, decode, compare) opens the appropriate file(s) for reading and/or writing. Inside the functions themselves (so not in main). At least, that is what I understood from your paraphrasing of the assignment. Could you post the exact and complete text of the assignment ?
0
iqlooAuthor Commented:
Here's the exact assignment:

Implement class Code and write a driver that:

1) Opens the file "cipher.txt" and reads the values into the array "char cipher[26]" which is the substitution cipher used to encode a simple text file.

2) Opens a file "raw.txt" and reads the contents one line at a time; removes all the characters that are not either alphabetic or space; and writes the prepared text to the file "source.txt".

3) Reads the file "source.txt" one line at a time; encodes the text using the cipher; and writes the encoded text to the file "encrypted.txt"

4) Uses the given function "inverseCipher" to generate the decoding substitution.

5) Decrypts the file "encrypted.txt" one line at a time and writes the decrypted text to the file "verify.txt".

6) Compares the text in the files "source.txt" and "verify.txt" to make sure they are the same.

7) For extra credit you may create a function "generateCode" that creates a random permutation of the alphabet and a default constructor.

We are also given the class definition.

Thanks for the help. Sorry if my first question wasn't clear.
#ifndef _CODE_H
#define	_CODE_H

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>

using namespace std;

class Code
{
public:
    Code();
    Code(char[]);
    void prepare(ifstream&, ofstream&);
    void generateCode();
    char encode(char);
    char decode(char);
    void inverseCipher();

private:
    char cipher[26];
    char decipher[26];
};


#endif	/* _CODE_H */

Open in new window

0
Infinity08Commented:
Ok. That changes things slightly. It seems that all file I/O happens in the main code.

Good. So, how far did you get ? Did you read the tutorial ? Is there anything that is still not clear ?
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

iqlooAuthor Commented:
Yes the tutorial helped a lot. I'm still confused on a few things though. My understanding is that you can have two files open at a time; one in "ifstream" and one in "ofstream". Is that correct? If so, would the "prepare" function below be proper?  
void Code::prepare(ifstream& inFile, ofstream& outFile)
{
    char ch;
    while((ch = inFile.get()) != EOF)
    {
        if(isalpha(ch))
        {
            ch = tolower(ch);
            outFile << ch;
        }
        
        if(isspace(ch))
            outFile << ch;
        else
            continue;
    }
}

Open in new window

0
Infinity08Commented:
>> My understanding is that you can have two files open at a time;

You can have more than two files open at the same time if you want. The limit is imposed by the underlying OS, but is certainly sufficient for your needs.

>> one in "ifstream" and one in "ofstream".

ifstream and ofstream are types. You can create as many instances of those types as you want.


>> If so, would the "prepare" function below be proper?  

Except for the comparison with EOF, that looks fine.

To check for the end of the file, you should check whether the stream is still ok. For example this way :
while (inFile.get(ch)) {
    // ...
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
iqlooAuthor Commented:
Ah ok. That makes so much more sense. I think I should be able to figure it our from here. Thanks for your help!
0
Infinity08Commented:
No problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.