Book Cipher

Hi, I am new to the world of C++. I am take a computer programming course in college and I have this project of creating a book cipher. I'm stuck here, and I was wondering if anyone could help me out?

Thanks Mike

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
46
47
48
49
50
51
52
53
54
55
	/* bcencode.cpp */
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[]){

	// declare vector 
	vector<char> bookVector;
	vector<char> messageVector;
	vector<int> codedVector;
	
  ifstream bookInput("bookfile.txt");
	if (!bookInput) {
		cerr << "Could not open the file" << endl;
		return EXIT_FAILURE;
	}

	// load the book into the the vector
	char book;
	while (!bookInput.eof()) {
		bookVector.push_back(bookInput.get());
	}

	ifstream messageInput("messagefile.txt");
	if (!messageInput) {
		cerr << "Could not open the file" << endl;
		return EXIT_FAILURE;
	}
	// load the message into the the vector
	char message;
	while (!messageInput.eof()) {
		messageVector.push_back(messageInput.get());
	}

	ofstream codedfileStream("codedfile.txt");
	if (!codedfileStream) {
		cout << "Could not open the output file" << endl;
		return EXIT_FAILURE;
	}
  
  vector<vector< unsigned> >indexLocation;
  
	for (vector<char>::size_type i = 0; i < messageVector.size(); ++i) {
		for (vector<char>::size_type k = 0; k < bookVector.size(); ++k) {
			if (bookVector[k] == messageVector[i]) {
				indexLocation[i].push_back(k);
				codedfileStream << k << " ";
				break;
			}
		}
	}
Last edited on
Help you out with what exactly?
here is the problem I am having. I have it so that it reads the book file and the message file. then it takes the message and finds the index value in the bookfile and it does that well. The message I am using is "hello", it puts all the correct index values (I counted it myself) for each letter into the codedfile, but it there is 6 index values. The last value is 379 which equates to the letter "e", but in my book there are 384 index's, and the code just gets the first index with that letter it find, it doesn't pick random index's. So basically what is puts to the codedfile is, 32 2 35 35 14 379, based on my bookfile

So it ouputs one extra index for some reason?
Last edited on
Topic archived. No new replies allowed.