Ok so i made a decoder for this that gets the letters from the numbers that this encoder gets. Problem is that i think the numbers its giving me are wrong and i can't seem to figure out whats causing the problem. Help please! :(

#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 << "\nRandom number is: " << startingPoint << endl;
			cout << "\nmsgVector is: " << msgVector[i] << endl;
			cout << "\nc 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;
				if (msgVector[i] == c)
				{
					//print number of char in bookFile to outFile, print a space before next char.
					out << ii << " ";
					break;
				}
			}//end for
		}//end for

	}//end stream

	cin.ignore();

	return 0;
}//end main

Recommended Answers

All 2 Replies

We can't run it since you haven't provided the the input file(s). What exactly is the program supposed to do? For particular input file(s), what is it SUPPOSED to output and what DOES it output?

It inputs a book file (txt) and a msg file (txt) and outputs a txt with number that are the "offset" of the characters in the book file.

I've attached the files i'm using for the book and msg (all though they can be what ever you want)

To run the program:

1. Open cmd prompt
2. set you path to the file the exes are located in
3. type project2_2.exe book.txt msgFile.txt outfile.txt
4.type project2_2D.exe book.txt outfile.txt newMsg.txt

3. = the encoder takes in the book file msgfile and puts the numbers in the outfile
4. = the decoder, rewrites the msg from the outfile to the newmsg

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.