Page 1 of 1

Would Like Some Pointers.. Encoding Using Arrays - C++

Posted: Wed Aug 16, 2006 11:50 am
by matt1019
Hello Guys,

Can one of you gurus take a look at this one.... lets just say its not behaving the way it supposed to.

This what it should do:
Program that encodes the text in a data file using a character array named key that contains 26 characters. This key is read from the keyboard. The first letter contains the character that is to replace the letter "a" in the data file, the second letter contains the letter that is to replace the letter "b" in the data file.... and so on. All punctuation is to be replaced by spaces.

and here's my beautiful code. Please AVOID the decode function.... as soon as I can get the ENCODE function working, decoding wouldn't be a problem.... or atleast that's what I think ;)

So without further Adue,

Code: Select all

/*
  ##################################################################
  ##NAME:                 RUSHIKUMAR J. BHATT                      #
  ##CLASS:                CSE 123C                                 #
  ##DAY/DATE:             WEDNESDAY/August 16, 2006                   #
  ##PROGRAM NAME/Desc.:   Encode/Decode pg 281#
  ##################################################################
*/

//need this for output/input
#include <iostream>
//need this lib. for opening and writing files
#include <fstream>
//need this lib. for checking if what we are reading is alphanumaric or not
#include <cctype>
//need this for joining our encoding/decoding
#include <string>

//nothing new here ;)
using namespace std;

//This is our one of the main engines of the whole program
//this part right here, will ENCODE the Alpha-Num characters and offset
//them by two (two the right)
char EncodeElse(const char Else)
{
	switch(Else)
	{
	default: return(' ');
	}
}

//This is our one of the main engines of the whole program
//this part right here, will ENCODE the Alpha-Num characters and offset
//them by two (two the right)
char EncodeChar(const char Alpha, char MyArray[])
{

	switch(Alpha)
	{
	case 'a': return(MyArray[0]);
	case 'b': return(MyArray[1]);
	case 'c': return(MyArray[3]);
	case 'd': return(MyArray[4]);
	case 'e': return(MyArray[5]);
	case 'f': return(MyArray[6]);
	case 'g': return(MyArray[7]);
	case 'h': return(MyArray[8]);
	case 'i': return(MyArray[9]);
	case 'j': return(MyArray[10]);
	case 'k': return(MyArray[11]);
	case 'l': return(MyArray[12]);
	case 'm': return(MyArray[13]);
	case 'n': return(MyArray[14]);
	case 'o': return(MyArray[15]);
	case 'p': return(MyArray[16]);
	case 'q': return(MyArray[17]);
	case 'r': return(MyArray[18]);
	case 's': return(MyArray[19]);
	case 't': return(MyArray[20]);
	case 'u': return(MyArray[21]);
	case 'v': return(MyArray[22]);
	case 'w': return(MyArray[23]);
	case 'x': return(MyArray[24]);
	case 'y': return(MyArray[25]);
	case 'z': return(MyArray[26]);
	default: return(char(Alpha+2));
	}
}

//This is our second of the main engines of the whole program
//this part right here, will DECODE the Alpha-Num characters and offset
//them by two (two the left)
char DecodeChar(const char Alpha)
{
	switch(Alpha)
	{
	case 'a': return('y');
	case 'A': return('Y');
	case 'b': return('z');
	case 'B': return('Z');
	case '0': return('8');
	case '1': return('9');
	default: return(char(Alpha-2));
	}
}


//So, lets get started?
int main()
{
	//let's declare our int to get from user input (Menu)
		int ans;
		//Lets ask the user to choose
		cout<<"Please Select From The Following Choices: "<<endl;
		cout<<"[1] Encode A File"<<endl;
		cout<<"[2] Decode A File"<<endl;
		cout<<endl;
		cout<<endl;
		cout<<"Enter 1 OR 2: ";
		//Get their Choice and assign it to ans
		cin>>ans;

		//if the user wants to Encode
		if (ans == 1){
			char keys[26];

			cout<<"Enter the keys Please: ";
			cin.getline(keys,27);

			//open a file called datain.dat
			ifstream InFile("datain.dat");

			//this file will be used for writing what we encode
			fstream EncFile("dataenc.dat");

			//lets catch the "file fail" part... using a different tech. then cerr
			//but gets the job done ;)
			if(InFile.fail()){
				cout<<"File is not there, buster!"<<endl;
				cout<<endl;
				cout<<endl;
				cout<<"Make sure that whatever you want to Encode"<<endl;
				cout<<"resides in a file called datain.dat"<<endl;
				return(0);

			//If file open is a success, lets move on:
			}else{
				//declare a char that will be used in reading the InFile datain.dat
				char letter;
				//the string
				string newsequence;
				//now, lets parse each char from the file.
				//it's a while loop so, until it reaches the end of file...
				while(!InFile.eof() && InFile.get(letter)){
				if (isalpha(letter)){
					newsequence += EncodeChar(letter, keys);
				}else if(!isalpha(letter)){
					newsequence += EncodeElse(letter);
				}
			}

			//Sorry, I had to do something fancy.... haha
			system("cls");
			
			//lets show-off ;)
			//let the user know what we were able to do
			cout<<"This is what I Encoded: "<<endl;
			cout<<newsequence<<endl;
			cout<<endl;
			cout<<endl;
			cout<<"Now I am going to write this to a file called"<<endl;
			cout<<"dataenc.dat"<<endl;

			//This is where the writing occurs ;)
			EncFile << newsequence;
		}

		//OK, now if the answer choice was to "DECODE"
		//then do that
		}else if(ans == 2){

			//notice, this is the same file that we stored our encoded text in
			ifstream InFile("dataenc.dat");

			//use a new file to write the decoded stuff
			fstream DecFile("datadec.dat");

			//same as above.... if file is not there part ;)
			if(InFile.fail()){
				cout<<"File is not there, buster!"<<endl;
				cout<<endl;
				cout<<endl;
				cout<<"Make sure that whatever you want to Decode"<<endl;
				cout<<"resides in a file called dataenc.dat"<<endl;
				return(0);

			//OK, File open is a success, lets move on
			}else{
				char letter;
				string newsequence;
				while(!InFile.eof() && InFile.get(letter)){
				if (isalpha(letter)){
					newsequence += DecodeChar(letter);
				}
			}
			system("cls");
			cout<<"This is what I Decoded: "<<endl;
			cout<<newsequence<<endl;
			cout<<endl;
			cout<<endl;
			cout<<"Now I am going to write this to a file called"<<endl;
			cout<<"datadec.dat"<<endl;

			//Writing what we decoded to a file called datadec.dat
			DecFile << newsequence;
		}
		}

		//Hahahahaa, I am still laughing on the following output!!!
		//try inputing anything else besides 1 or 2 and you will see
		//a mad/ticked-off program!
		else{
			cout<<"Are You Even Trying?"<<endl;
		}

		//Lets exit out peacefully

		return(0);
}
//RushiKumar EXIT ;)
ok, so it can open the file correctly (datain.dat is the file where A sample text to be encoded is written)
but everytime I get pass the menu, it skips the "Entering of the keys" part.... and of course, does not encode.

Any help/suggestions/advice/tip/pointers....?

Any help would be appreciated!

thanks,

-Matt

No help?

Posted: Wed Aug 16, 2006 3:44 pm
by matt1019
No help? ahhh... i know, i cant deal with arrays either!!

Thank god this was the last assignment ;)

thanks anyways guys.

-Matt


Weirdan | Bump in a 24 period is a no-no!
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not recieve a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.

Posted: Wed Aug 16, 2006 3:47 pm
by feyd
too much code for my patience to try to wade through. :)

Posted: Wed Aug 16, 2006 4:45 pm
by matt1019
I understand.

Thank you anyways feyd ;)

-Matt