Home | | Syllabus | | Assignments | | Lectures | | Resources
//********************************************************
// proj3.cc
// Author: A good solution writer
// Date: March 21, 2014
// Class: CSCI 131 project3, Professor Royden
// given by the user.
// Input: (from keyboard) Inputs a filename and key from the keyboard.
// (from data file) Inputs message to be encrypted
// Output: (to encoded.txt file) Outputs encrypted text
// (to report.txt file) Outputs number of characters encoded
// and number not encoded.
//********************************************************
#include <iostream> //iostream libraries
#include <fstream>
#include <iomanip>
using namespace std;
int main(void)
{
int key; //key used for encryption
ifstream myInputFile; //input file stream for message
ofstream myOutFile; //output file stream for encrypted message
int countCiphers = 0; //counter for encrypted characters
int countOther = 0; //counter for other characters
char ch; //variable to read in each character.
//Get input file and key
getInputFile(myInputFile);
cout << "Please enter the key to use: ";
cin >> key;
cout << "So, the key is " << key << endl << endl;
key = key % 26; //Adjust key if > 25, so wraps around every 26
myOutFile.open("encoded.txt");
//get each character and encrypt it
myInputFile.get(ch);
while(myInputFile) {
ch = tolower(ch);
if (97 <= (int)ch && (int)ch <= 122){ //if alphabet character
countCiphers++;
if ((int)ch + key > 122){ //if encryption goes past 'z'
ch = (char)((int)ch + key - 26); //wrap around
} else {
ch = (char)((int)ch + key);
} //end if for wrap around
} //end if for alphabet character
else {
countOther++;
} //end else (other characters)
myOutFile << ch; //output encrypted character
myInputFile.get(ch); //get next character
} //end while (more characters)
myInputFile.close( ); //close input file
myOutFile.close( ); //close output file
cout << "Message is encoded in file encoded.txt." << endl;
cout << "Please also check report.txt." << endl << endl;
writeReport(countCiphers, countOther); //Write statistics report
return 0;
} //end main( )
//*************************************************************************
// void getInputFile(ifstream& myInputFile)
// Purpose: Gets input filename from user, opens it and tests if it's good,
// repeating until the input file is determined to be good.
// Input: (from keyboard) Input file name
// Output: (to myInputFile param) input file stream
// Pre: None
// Post: an input file name has been read in and opened and associated
// with the input file stream, myInputFile
//************************************************************************
void getInputFile(ifstream& myInputFile) {
char inputFileName[31]; //Input file name
//Get input filename from user
cout << "Please enter the data file name: ";
cin >> inputFileName;
cout << "You typed " << inputFileName << endl;
myInputFile.open(inputFileName); //Open input file
while(!myInputFile) { //while open failed
cout << "** ERROR: The file, " << inputFileName
<<" does not exist." << endl; //Output error message
cout << "Please enter the data file name: "; //Get a new filename
cin >> inputFileName;
cout << "You typed " << inputFileName << endl;
myInputFile.open(inputFileName);
} //end while(!myInputFile)
return;
} //end getInputFile( )
//*************************************************************************
// void writeReport(int countLetters, int countNonLetters)
// Purpose: Write a report to a file with statistics of encryption
// Output: (to report.txt) Number of letters encoded and
// number of other characters.
// Pre: countLetters and countNonLetters have been assigned
// Post: the number of letters and nonLetters have been output to the
// file, report.txt
//*************************************************************************
void writeReport(int countLetters, int countNonLetters) {
ofstream outFile; //output file stream
outFile.open("report.txt");
outFile << "In the text from the input file:"
<< endl << endl;
outFile << setw(40) << "The number of letters encoded was: "
<< countLetters << endl;
outFile << setw(40) << "The number of other characters was: "
<< countNonLetters << endl << endl;
outFile.close( );
} //end writeReport( )
Home | | Syllabus | | Assignments | | Lectures | | Resources
Constance Royden--croyden@cs.holycross.edu
CSCI 131: Techniques of Programming
Last Modified: April 4, 2014
Page Expires: October 12, 2014