| Previous | Table of Contents | Next |
Listing 6.6 The RANDOM2.CPP program listing and its repeated execution using different random seed numbers.
/*
random2.cpp
C++ code written by Jonathan Held on April 28, 1998,
using Microsoft Visual C++ version 5.0
*/
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
//function prototypes
bool checkInput(char *&);
char* encipher(char *&);
void formatData(char []);
void getMessage(char *&);
unsigned int getSeed(void);
void gotit(const char [], char *&, const int, int);
const int BUFFER_SIZE = 256;
//----------------------------------------------------------------
//Function: main()
//Parameters: None
//Return Type: int - 0 if program terminated normally
//Purpose: driver that makes calls to various procedures defined
//in random1.cpp
//----------------------------------------------------------------
int main(){
unsigned int seed;
char *message, *result;
while (seed = getSeed()) {
srand(seed);
getMessage(message);
cout << "\nYou entered: " << message << endl;
result = encipher(message);
cout << "Enciphered message is " << result << endl << endl;
delete message;
}
return 0;
}//end main()
//----------------------------------------------------------------
//Function: checkInput()
//Parameters: input - the message the user entered
//Return Type: bool - true if the input string contains an error,
// false otherwise
//Purpose: Checks the user's keyword for invalid characters.
//----------------------------------------------------------------
bool checkInput(char * &input)
{
bool error = false;
int count = strlen(input);
for (int ix=0; ix<count; ix++){
int char_value = static_cast<int>(*(input+ix));
//determine if the user did not enter an uppercase character
if ((char_value < 65) || (char_value > 90)){
error = true;
cerr << "\aYou entered an invalid message!" << endl << endl;
break;
}
}
if (count == 0){
cerr << "\aYou entered an invalid message!" << endl << endl;
error = true;
}
return error;
}//end checkInput()
//----------------------------------------------------------------
//Function: encipher()
//Parameters: inp - the user's input that will be enciphered
//Return Type: char * - a pointer to the encrypted string
//Purpose: returns a pointer to the encrypted text
//----------------------------------------------------------------
char* encipher(char *&inp){
//yet another, and easier way to represent the plaintext
//array
const char plaintext[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int index = 0;
char *encrypted = new char[strlen(inp)+1];
assert(encrypted);
for (int ix=0; ix < strlen(inp); ix++){
for (int jx=0; jx <26; jx++){
if (plaintext[jx] == inp[ix]){
gotit(plaintext, encrypted, jx, index++);
break;
}
}
}
//don't forget to insert the null terminator
encrypted[strlen(inp)] = '\0';
return encrypted;
}//end encipher();
//----------------------------------------------------------------
//Function: formatData()
//Parameters: data - the array we want to format
//Return Type: None
//Purpose: Get rid of all spaces in the array.
//----------------------------------------------------------------
void formatData(char data[]){
for (int mx=0, nx=0; (*(data+nx) != '\0'); nx++){
if (*(data+nx) == ' '){
//do nothing - skip over the space in the data
}
else {
*(data+mx++) = *(data+nx);
}
}
//don't forget to add the null terminator
*(data+mx) = '\0';
return;
}//end formatData()
//----------------------------------------------------------------
//Function: getMessage()
//Parameters: input - user's one-line message
//Return Type: None
//Purpose: Gets a one-line message from the user.
//----------------------------------------------------------------
void getMessage(char *&input){
char buffer[BUFFER_SIZE] = {'\0'};
bool error = false;
do{
cout << "Enter a one-line message (no spaces) in UPPERCASE: ";
cin.getline(buffer, BUFFER_SIZE, '\n');
assert(input = new char[strlen(buffer) + 1]);
strcpy(input, buffer);
formatData(input);
error = checkInput(input);
if (error)
delete input;
} while (error);
return;
}//getMessage()
//----------------------------------------------------------------
//Function: getSeed()
//Parameters: none
//Return Type: unsigned int - the seed the client entered
//Purpose: gets the seed for the random number generator. If the
//user enters a negative number, it causes the seed to underflow to
//a very high non-negative number. If the user enters an
//alphanumeric sequence, e.g., "A123", then seed_Number is set to
//zero.
//----------------------------------------------------------------
unsigned getSeed(){
unsigned int seed_Number;
cout << "Enter seed, 0 (or non-digit) to terminate: ";
cin >> seed_Number;
cin.ignore();
return seed_Number;
}//end getSeed()
//----------------------------------------------------------------
//Function: gotit()
//Parameters: p_text - the plaintext array, i.e., characters "A-Z"
// enc_msg - a pointer to the message that will be encoded
// loc - the location of each encoded character in the
// plaintext array
//Return Type: None
//Purpose: encodes the user's input using random numbers
//----------------------------------------------------------------
void gotit(const char p_text[], char *&enc_msg, int loc, int index){
int random_num, z;
do {
random_num = rand() % 100;
} while (random_num > 25);
z = (loc + random_num) % 26;
enc_msg[index] = p_text[z];
return;
}//end gotit()
//end file random2.cpp
A:\c>RANDOM2
Enter seed, 0 (or non-digit) to terminate: 5
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is TDYLWCANCKC
Enter seed, 0 (or non-digit) to terminate: 8
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is QLEGOKYPGRV
Enter seed, 0 (or non-digit) to terminate: 51
Enter a one-line message (no spaces) in UPPERCASE: FIREFREDNOW
You entered: FIREFREDNOW
Enciphered message is KIYOSXAWQTW
Enter seed, 0 (or non-digit) to terminate:
| Previous | Table of Contents | Next |