Ok, something very wrong is going on with my program. When i run it the numbers it is giving me are sometimes right but are also wrong. H (the first letter in the word) is both = to 0 and 1 (wtf?). Any ways below i'll show you want i should be getting and what i am getting.

Book File (12 characters) Hello world! Message File (12 characters) Hello world! Out File (generated from the encoder, should be 12 numbers) 0 1 2 3 4 5 6 7 8 9 10 11 when the out file is decoded it should be the same as the message file.

this is what I'm getting:

Book File (12 characters) Hello world! Out File (generated from the encoder, should be 12 numbers) 1 3 9 7 5 6 7 9 10 11 when decoded this comes out to: Hlrwo wrldd please help me find out whats causing this problem!
Below is my code for each program.

ENCODER

#define _CRT_RAND_S
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;

int getSize (istream& file)
{
	int num = 0;
	char c =file.get();
	while (file)
	{
		++num;
		c = file.get();
	}
	return num;
}

int random(int low, int high)
{
	unsigned num;
	rand_s(&num);
	double normalized = static_cast<double>(num) / UINT_MAX;
	return static_cast<int>(normalized * (high - low +1)) + low;
}

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: encode.exe bookFile.txt msgFile.txt outFile.txt" << endl;
	}
	else
	{
		ifstream bookIn(argv[1]);
		ifstream msgIn(argv[2]);
		ofstream out(argv[3]);
		vector<char> msgVector;
		while (msgIn)
		{
			msgVector.push_back(msgIn.get());
		}
		msgIn.clear();
		msgIn.seekg(0, ios::beg);

		//count number of chars in the book file and set the char size
		int bookSize = 0;
		int msgSize = 0;
		msgSize = getSize(msgIn);
		msgIn.clear();
		msgIn.seekg(0, ios::beg);
		bookSize = getSize(bookIn);
		bookIn.clear();
		bookIn.seekg(0, ios::beg);
		int startingPoint = 0;
		char c = bookIn.get();

		for (int i=0; i < msgSize; ++i)
		{
			bookIn.clear();
			bookIn.seekg(0, ios::beg);
			//select a random number from the generator
			startingPoint = random(0,bookSize);

			//go to that number in the book
			for (int ii=0; ii < startingPoint; ++ii)
			{
				c = bookIn.get();
			}
			cout << "Random number is: " << startingPoint << endl;
			cout << "msgVector is: " << msgVector[i] << endl;
			cout << "c is: " << c << endl;
			//find the next char that is = to the char of the msg
			for(int ii=startingPoint; ii < bookSize; ii++)
			{
				c = bookIn.get();
				cout << "\nc is: " << c << endl;
				cout << "ii is at: " << ii << endl;
				cin.ignore();
				if (msgVector[i] == c)
				{
					//print number of char in bookFile to outFile, print a space before next char.
					out << ii << " ";
					break;
				}
				else if (ii == (bookSize-1))
				{
					cout << "No character found" << endl;
					--i;
					ii=0;
					break;
				}
			}//end for
		}//end for

	}//end stream

	cin.ignore();

	return 0;
}//end main

DECODER

#define _CRT_RAND_S
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;


int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: decode.exe bookFile.txt codedFile.txt outFile.txt" << endl;
	}
	else
	{
		ifstream bookIn(argv[1]);
		ifstream codeIn(argv[2]);
		ofstream out(argv[3]);

		int temp;
		vector<int> codedNum;
		while (!codeIn.eof())
		{
			codeIn >> temp;
			codedNum.push_back(temp);
		}
		codeIn.clear();
		codeIn.seekg(0, ios::beg);
		//for(int i=0; i < codedNum.size(); ++i)
		//cout << codedNum[i] << endl;

		int num = 0;
		char c = bookIn.get();
		//bookIn.clear();
		//bookIn.seekg(0, ios::beg);

		for(int i=0; i < codedNum.size(); ++i)
		{
			bookIn.clear();
			bookIn.seekg(0, ios::beg);
			num = 0;
			while (num != codedNum[i])
			{
				c = bookIn.get();
				++num;
			}

			out << c;

			cout << "num is: " << num << endl;
			cout << "letter is: " << c << endl;
		}//end for

		

	}//end stream

	cin.ignore();

	return 0;
}//end main

It looks like you have a bunch of debugging info in the program, which is fine for debugging purposes, I suppose, but it means it's not entirely clear to the outside observer what the actual code is and what is debugging code that just hasn't been taken out yet.

I had to change the random number code so I could compile it ( rand_s isn't a standard function). I'm confused as to why you have any random numbers in there at all. Either that or I don't understand how the "book" and "message" are supposed to interact to encode the message. Are you supposed to have a different coded message every time you have the same book and plaintext message? If you aren't, what's random?

The encoding algorithm isn't clear to me, so I can't hazard a guess as to what is going wrong in the C++ code.

All the you can ignore any cout as they do nothing but debugging. I'm using a random number so the coded message will be different each time you run the program.

Ok I've done some more tests and found out that the encoder will read one last space at the end of the book file(that needs to be fixed), but it does get the right numbers. Therefor the main problem is the decoder. I've noticed that it takes bath 0 and 1 as H but i would like it to start at 0 being H and 1 e 2 l and so on. the decoder also repeats the last character.

All the you can ignore any cout as they do nothing but debugging. I'm using a random number so the coded message will be different each time you run the program.

You want the coded message to be different each time? That means that "Hello World!" maps to more than one coded message and more than one coded message maps to "Hello World!"? I, for one, don't understand the coding and decoding algorithm you are trying to use.

The coded message is different each time because the real file i am using is a book to code and decode the message. It would also be stupid to not use all your characters you have, by using them all it makes it harder to notice repeating numbers in the coded file and therefor harder for someone to decode.

The coded message is different each time because the real file i am using is a book to code and decode the message. It would also be stupid to not use all your characters you have, by using them all it makes it harder to notice repeating numbers in the coded file and therefor harder for someone to decode.

I would make life easier on myself. Have a book file like:

abcdefghijklm !nopqrstuvwxyz

Each letter and punctuation mark shows up once and all messages map to the same message. Get THAT to code and decode correctly without the random numbers, then when THAT works flawlessly, throw some repeats into your book file and randomize it it a little at a time. Start with messages that have no repeating letters in them.

Here is the exe's if you want to test and see how the program works. Read the README before use.

I've been running it already, but I'll look at the README and the files.

This article has been dead for over six months. Start a new discussion instead.