Question: #include #include #include ///Determines the number of letters on each rail, given a key and an encoded message, and separates ///the encoded message into an

#include #include #include

///Determines the number of letters on each rail, given a key and an encoded message, and separates ///the encoded message into an array of size key, representing the rails. The length of the cycle ///must be determined, along with the size of the remainder representing an unfinished cycle. void splitEncoded( std::string strarray[]){ std::string msg; int key; int cycle= (2 * key)-2; int div=cycle; int len=msg.length(); int remainder=0; int pos=0; for(int i=0; i

if(cycle!=0 && remainder!=0){ counter= 2*counter; if((len%div)>=(div-(i-1))){ counter+=2; } else if((len%div)>i){ counter++; } } else if(((len)%div)>i){ counter++; }

strarray[i]=msg.substr(pos,counter); pos+=counter; cycle=cycle-2; remainder+=2;

}

}

///Takes the filled array representing the rails and cycles down and up to ///to retrieve the chars in the correct order, building the decoded /// message. std::string concatDecoded( std::string strarray[], int len){ std::string decodedMsg; int counter=0; int key; while(counter1) strarray[i]=strarray[i].substr(1); else strarray[i]=""; ++counter;

} for(int i=key-2; i>0; i--){ if(counter==len){ return decodedMsg; } decodedMsg+=strarray[i][0]; if(strarray[i].length()>1) strarray[i]=strarray[i].substr(1); else strarray[i]=""; ++counter;

} } return decodedMsg; }

///returns a char converted to Uppercase

char toUpperCase (char c) { ///FILL THIS FUNCTION: converts char c to UPPER CASE and returns UPPER CASE. if(c>='a' && c<='z') c = c-32; else c = c;

return c; };

///Return true if char is alpha numeric

bool isAlphanumeric (char c) { ///FILL THIS FUNCTION: evaluates char c, returns true if c is Alphanumeric (0-9, A-Z, a-z) if(isalnum(c)){ return true; } else return false; ///this is just a place holder for your return statement. };

///Given an uncoded message and an array representing the rails ///cycles down and up the rails, filling the array until message ends void splitMessage( std::string strarray[]){ std::string msg; int counter=0; int key; while(counter0; i--){ strarray[i]+= msg[counter]; ++counter; if(counter==msg.length()) return; } }

};

///given a filled array representing the rails, concatenates the ///strings on the rails and returns a coded message std::string concatEncoded(std::string strarray[]){ ///FILL THIS FUNCTION: given an array of strings representing the rails, ///and the size of the array, ///concatenate the strings into a single string representing the encoded message and ///return it.

return "this is an encoded message";///this is just a place holder for your return statement }

///given a message, formats the message for encoding or decoding void prepmessage(){ ///FILL THIS FUNCTION: given a message, converts message to all ///uppercase and removes non-alphanumeric characters including spaces ///and punctuation. Notice this is a VOID function!!!

} ///makes functions calls for encoding a message void encodeMessage(){ std::string msg; int key; prepmessage(); std::string encoded[key];//an array representing the rails splitMessage(encoded); msg=concatEncoded(encoded);

};

///makes function calls to decode a coded message void decodeMessage(){ std::string msg; int key; std::string decoded[key];//an array representing the rails splitEncoded(decoded); msg=concatDecoded( decoded, msg.length()); }

///********************************************** ///EVERYTHING BELOW THIS LINE REMAINS IN RAILFENCE.CPP ///**********************************************

///reads message from file stream in

std::string readMessage(std::ifstream& in) { std::string inmsg; std::string line; in.open("message.txt"); getline (in, line); while (in) { inmsg = inmsg + line + " "; getline (in, line); }

return inmsg; }

///given an out stream, prints the message

void printMessage(std::ifstream& out){ std::string msg; out.open("decode.txt"); out<

}

///promps the user for a KEY 2-5

int getKey(){ int key; int newkey; while(true){ std::cout<<"Select a Key (2-5): "; if(std::cin>>newkey){ if(newkey>1 && newkey<=5) break; } std::cout<<" Please enter a valid key!! "; std::cin.ignore(); } std::cin.ignore(); return key; };

///prints a menu and asks the user for a selection

char getselection(){ std::string selection="0"; std::cin.clear(); while(selection[0]<'1' || selection[0]>'3'){ std::cout << " *****Welcome to Rail Fence Coder***** Menu: " << "\t1 - encrypt a file " << "\t2 - decrypt a file " << "\t3 - quit " <<"Selection: ";

getline(std::cin, selection);

if(selection[0]<'1' || selection[0]>'3'){ std::cout<<" Please enter a valid selection!!"<

int main() { int key; std::string msg; std::ifstream inFile("message.txt"); std::ifstream inFile2("encoded.txt");

bool quit=false; while(!quit){ char select=getselection(); switch(select){ case '1': key = getKey(); msg=readMessage(inFile); printMessage(std::cout); encodeMessage(); printMessage(std::cout); break;

case '2': key = getKey(); msg=readMessage(inFile); printMessage(std::cout); prepmessage(); decodeMessage(); printMessage(std::cout); break;

case '3': std::cout<<" Exiting Rail Fence Coder "<

} }

return 0; }

I need help with the functions that say fill in and converting this code from cin to fstreams. Thanks. Also for the fstream infile, it can be any text like "Have a nice day"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!