/* Implements a Caesar cipher for encrypting and decrypting a string message (see ch. 4 in D'Orazio). Includes the syntax for getline using string objects (note the difference with using getline with C strings). Also note the integer arithmetic to implement the cipher. */ #include #include #include #include void get_data (string&,int&); void cipher(string&,int); void main(){ string message; int offset; get_data(message, offset); cipher (message, offset); cout << "The encrypted message is:" << endl; cout << message << endl << endl; cipher (message, -offset); cout << "The deciphered message is:" << endl; cout << message << endl << endl; } void get_data(string& m, int& n){ cout << "Enter several lines of text terminated by a #:" << endl; getline(cin,m,'#'); /* reads in data until a '#' is found, instead of waiting for a '\n' */ cin.ignore(2000,'\n'); cout << "Enter an integer offset: "; cin >> n; } void cipher(string& m, int n){ int len, i; n = ((n%26)+26)%26; /* ensures n is nonnegative */ len = m.length(); for(i=0; i