Caesar Cipher Help needed (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.

Header File (we can not make changes to this file at all)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  // 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 that I have created, as you can probably tell I dont know what I am doing. I am confused as to how to use the pointer properly from the header file and also I dont feel like I am initiating my object properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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 + 5) % 26;
		c_alphabet[i] = std_alphabet[k];
	}

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

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

};
#endif 



This is my test file.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <iostream>
#include "Caesar.h"

int main() {

	Caesar test(5);

	cout << test;

	system("PAUSE");
	return 0;
};
The first thing I see is that you have include guards in your source file, they are not required in source files, only headers should have include guards.

Second, you have pointers so you need to allocate or assign memory to these pointers.



thanks jlb, how do I initialize the pointers as arrays in my .cpp file?
You either need to allocate memory with new, or assign the pointer to some valid memory.
closed account (48T7M4Gy)
main line 9
cout << test;

You need an overloaded << operator in your class for this to work.

It might be easier at this stage to get the arrays right first, and then write an appropriate get method, and after that address operator overloading later.
closed account (48T7M4Gy)
PS the (2) get methods would return the std_alphabet and the c_alphabet like test.getAlphabet() and test.getCAlphabet(). Once you get them you can print them out with a loop.
Topic archived. No new replies allowed.