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 ;)
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