Link to home
Start Free TrialLog in
Avatar of iqloo
iqloo

asked on

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

>>  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 ?
Avatar of iqloo
iqloo

ASKER

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

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 ?
Avatar of iqloo

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iqloo

ASKER

Ah ok. That makes so much more sense. I think I should be able to figure it our from here. Thanks for your help!
No problem.