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?
/* bcencode.cpp */
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
}
}
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