course.wilkes.edu/CS125Labs
Welcome to the site for CS 125 labs!

 

encode.cpp

Prev | Next | Lab 8

// encode.cpp allows its users to encode a message stored in a file
//  using the Caesar cipher.
//
// The decoded message is stored in a file.
//
//  Begun by: Joel C. Adams, for Hands On C++.
//  Completed by:
//  Date:
//  
//  Specification:
//    input(input file):   a sequence of characters.
//   output(output file): the sequence of encoded input characters.
//*********************************************************************

#include <iostream> // stream I/O #include <fstream> // ifstream, ofstream #include <string> // string #include <cassert> // assert() using namespace std;

char CaesarEncode(char ch, int key);

int main() { // 0. Display introductory message cout << endl << "This program uses the Caesar cipher to encode the contents of a" << endl <<"file and writes the encoded characters to another file." << endl; // 1. Prompt for and read name of the input file. cout << endl << "Enter the name of the input file: "; string inFile; cin >> inFile;

// 2. Open an ifstream named inStream for input from inFile

// 3. If inStream failed to open, display an error message and quit assert( /* inStream opened successfully */ );

// 4. Prompt for and read name of the input file. cout << endl << "Enter the name of the output file: "; string outFile; cin >> outFile;

// 5. Open an ofstream named outStream for output to outFile

// 6. If outStream failed to open, display an error message and quit assert( /* outStream opened successfully */);

char inChar, outChar; // 7. Loop for (;;) { // a. read a character from the input file via inStream into inChar

// b. if end-of-file was reached, terminate repetition

// c. encode the character using the Caesar cipher outChar = CaesarEncode(inChar, 3); // d. write the encoded character to the output file via OutStream

}

// 8a. close the connection to the input file

// 8b. close the connection to the output file

// 9. display a 'successful completion' message cout << endl << "Processing complete." << endl << "Encoded message is in " << outFile << endl; }

//******************************************************************** // CaesarEncode implements the Caesar cipher encoding scheme. * // * // Receive: ch, a character. * // key, the amount by which to rotate ch. * // Return: The character that is key positions after ch, * // with "wrap-around" to the beginning of the sequence. * //******************************************************************** #include <cstdlib> // exit() #include <cctype> // isupper(), islower()

char CaesarEncode(char ch, int key) { const int FIRST_UPPER = int('A'), FIRST_LOWER = int('a'), NUM_CHARS = 26;

if (key <= 0 || key >= NUM_CHARS) { cerr << endl << "*** CaesarEncode: key must be between 1 and 25" << endl; exit(1); } if (isupper(ch)) return (ch - FIRST_UPPER + key) % NUM_CHARS + FIRST_UPPER; else if (islower(ch)) return (ch - FIRST_LOWER + key) % NUM_CHARS + FIRST_LOWER; else return ch; }

Prev | Next | Lab 8

 
  Home

Help

Lab 0

Lab 1

Lab 2

Lab 3a

Lab 3b

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13



Membership

Login




Last update: Thursday, November 1, 2001 at 8:29:24 AM.
visitors to this page.