简体   繁体   中英

Caesar Cipher C++ (arrays and pointers)

I am a complete beginner with c++ and up to this point in school we have only learned and used Java. Our first project this year is to create a caesar cipher but we must use the header file provided by our professor. At this point I am only trying to shift the letters and prove my concept before coding the encrypting and decrypting methods. Any help as to what I am doing wrong and why this code isnt compiling would be amazing.

This is our Header file, we are not allowed to make any changes to this file at all:

// include file for Caesar cipher code
//

#ifndef CAESAR_H
#define CAESAR_H

#include <string>

class Caesar {

private:
    //pointers used to refer to the standard alphabet array and the Caesar shift array
    char* std_alphabet;
    char* c_alphabet;

public:
    // The constructor . . . 
    // create the two arrays with the c_alphabet array contents representing the std_alphabet 
    // characters shifted by a value of the shift parameter
    Caesar(int shift = 0);

    // encrypt a message. Returns count of characters processed
    // first string is message, second is encrypted string
    int encrypt(const std::string& message, std::string& emessage);

    // decrypt a message. Returns count of characters processed
    // first string is encrypted string, second is decrypted string
    int decrypt(const std::string& message, std::string& dmessage);

    //delete any memory resources used by the class
    ~Caesar();

}; // end of class . . .
#endif

This is my .cpp file, I am only in the stage of currently trying to shift my alphabet in an array but I do not understand why my header file is using pointers or if I am creating my arrays properly(I was able to get this concept to work in a separate file but not using the header file). I am only trying to print the lines to make sure this works before going forward with any coding:

#ifndef CAESAR_C
#define CAESAR_C

#include <string>
#include <iostream>
#include "Caesar.h"

using namespace std;

int shift, i, k;
char letter = 'a';

Caesar::Caesar(const int n) {

    shift = n;
    std_alphabet[26];
    c_alphabet[26];

    for (i = 0; i < 26; i++) {

        std_alphabet[i] = letter;
        letter++;
    }

    for (i = 0; i < 26; i++) {

        cout << std_alphabet[i] << " ";
    }

    cout << endl;

    for (i = 0; i < 26; i++) {

        k = (i + shift) % 26;
        c_alphabet[i] = std_alphabet[k];
    }

    for (i = 0; i < 26; i++) {

        cout << c_alphabet[i] << " ";
    }


};
#endif

This is my test file, I dont really know how to initiate a Caesar object properly. Like I said I am a complete beginner with c++ and would greatly appreciate any directions:

#include <string>
#include <iostream>
#include "Caesar.h"

using namespace std;

int main() {

    Caesar* test = new Caesar(5);

    cout << test;

    system("PAUSE");
    return 0;
};

Please don't mind if my answer is vague since I'm quite new to participating in stackoverflow

It seems like you have not done much research on the issue or even tried to figure out the solution yourself before asking this question. But anyway I'll just try to be helpful to you.

Do these first (I guess already know these things):

  • First of all ofcourse, save your header and cpp files as Caesar.h and Caesar.cpp, respectively, and test.cpp maybe the test file.
  • Then include the header 'stdlib.h' in your test file since you're using the function system().
  • Then compile ur program using the command g++ Caesar.cpp test.cpp -o test and run it.

    Now, the main issue with ur code is at the line cout << test; . You have to overload the operator '<<' for the class 'Caesar' first which will print the values in the private member variables std_alphabet and c_alphabet . I suggest you take a tour of this page http://www.geeksforgeeks.org/overloading-stream-insertion-operators-c/
    Moreover, the design strategy of the class 'Ceasar' is not satisfactory. I mean the variable 'shift' in 'Caesar.cpp' should have been a private member variable of the class 'Caesar' which makes more sense. Anyway those are just OOP concerns.
    One more thing is that the constructor contains two lines: std_alphabet[26] and c_alphabet[26]
    Honestly, I don't know what's that weird syntax but it somehow gets compiled in my system. But that's what causes the program to crash during runtime. So I recommend just replacing those two lines with
    std_alphabet = new char[26]; and similar for c_alphabet .

    Finally, you just have to define the functions encrypt() and decrypt() in the file 'Ceasar.cpp' and I hope you can do that much since it is your assignment :D
    BEST OF LUCK !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM