Using getline() or something else that will count spaces.

Hi,

I'm obviously new to C++ (hence being in this forum) and I'm working on an enigma (encrypter/decrypter) project using arrays. I have the project almost completely finished, but I am having trouble with one thing. The program prompts the user to enter a message to be changed. When a capital letter is read, it is supposed to encrypt it through 2 rotors and rotate the rotors. When any other character is read, including spaces, it is supposed to pass them through unchanged and rotate the rotors. I was using "cin >> string val", but that was causing program suicide when I entered a space. I was trying to use getline(cin, val) but that runs an encryption before I enter a value. I'll post the code and if anyone can help that would be great.

Thanks!
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const char ORIGINAL_ROTOR_ONE[26] = {'M', 'Q', 'C', 'I', 'A', 'W', 'U', 'Y', 'L', 'G', 'E', 'B', 'R', 'T', 'D', 'H', 'O', 'X', 'V', 'J', 'F', 'Z', 'S', 'N', 'K', 'P'};
const char ORIGINAL_ROTOR_TWO[26] = {'B', 'N', 'Z', 'L', 'H', 'F', 'T', 'X', 'Y', 'J', 'K', 'U', 'D', 'R', 'A', 'V', 'I', 'M', 'P', 'O', 'C', 'Q', 'G', 'S', 'W', 'E'};

char encryptOneRotor (char toEncrypt, char (&passedRotor)[26], bool forward);
char decryptOneRotor (char toDecrypt, char (&passedRotor)[26], bool forward);
char encryptTwoRotors (char toEncrypt, char (&passedRotorOne)[26], char (&passedRotorTwo)[26]);
char decryptTwoRotors (char toDecrypt, char (&passedRotorOne)[26], char (&passedRotorTwo)[26]);
void rotateRotor(char (&passedRotor)[26], bool forward);

int main ()
{
char encryptingRotorOne[26];
char encryptingRotorTwo[26];
char decryptingRotorOne[26];
char decryptingRotorTwo[26];
int opt, length;
string val;



opt = 1;
while (opt != 3)
{
cout << "Please choose an option: " << endl;
cout << " 1.) Encrypt a message " << endl;
cout << " 2.) Decrypt a message " << endl;
cout << " 3.) Quit the program" << endl;
cin >> opt;
cout << "Your choice: " << opt << endl;
for (int i = 0; i < 26; i++)
{
encryptingRotorOne[i] = ORIGINAL_ROTOR_ONE[i];
encryptingRotorTwo[i] = ORIGINAL_ROTOR_TWO[i];
decryptingRotorOne[i] = ORIGINAL_ROTOR_ONE[i];
decryptingRotorTwo[i] = ORIGINAL_ROTOR_TWO[i];
}
if (opt == 1 || opt == 2)
{
cout << "Number of characters? " << endl;
cin >> length;
cout << "Enter message below: ";
getline(cin, val);
if (opt == 1)
{
for(int i = 0; i < length; i++)
{
cout << "ENCRYPTING LETTER: " << val[i] << endl;
cout << " CURRENT STATE OF ROTORS: " << endl;
cout << " ROTOR #1: ";
for( int j = 0; j < 26; j++)
{
cout << encryptingRotorOne[j];
}
cout << endl;
cout << " ROTOR #2: ";
for( int j = 0; j < 26; j++)
{
cout << encryptingRotorTwo[j];
}
cout << endl;
val[i] = encryptTwoRotors (val[i],encryptingRotorOne, encryptingRotorTwo);
cout << " ROTATING ROTORS!" << endl;

}
cout << "ENCRYTPED MESSAGE: " << val << endl;
}
else
{
for (int i = (length-1); i > -1; i--)
{
cout << "DECRYPTING LETTER: " << val[i] << endl;
cout << " CURRENT STATE OF ROTORS: " << endl;
cout << " ROTOR #1: ";
for( int j = 0; j < 26; j++)
{
cout << decryptingRotorOne[j];
}
cout << endl;
cout << " ROTOR #2: ";
for( int j = 0; j < 26; j++)
{
cout << decryptingRotorTwo[j];
}
cout << endl;
val[i] = decryptTwoRotors (val[i],decryptingRotorOne, decryptingRotorTwo);


}
cout << "DECRYTPED MESSAGE: " << val << endl;
}

}
}
if (opt == 3)
return (0);
}
char encryptOneRotor (char toEncrypt, char (&passedRotor)[26], bool forward)
{
char encryptedCharacter;
int position = toEncrypt - 'A';
encryptedCharacter = passedRotor[position];

rotateRotor(passedRotor, forward);


if (toEncrypt >= 'A' && toEncrypt <= 'Z')
{
return encryptedCharacter;
}
else if (toEncrypt == ' ')
{
toEncrypt = 32;

return toEncrypt;
}
else
{
return toEncrypt;
}
}

char decryptOneRotor (char toDecrypt, char (&passedRotor)[26], bool forward)
{
char decryptedCharacter;
int position = 0;

for (int i = 0; i < 26; i++)
{
if (passedRotor[i] == toDecrypt)
{
decryptedCharacter = 'A' + i;
}
}

rotateRotor(passedRotor, forward);

if (toDecrypt >= 'A' && toDecrypt <= 'Z')
{
return decryptedCharacter;
}
else
{
return toDecrypt;
}
}

char encryptTwoRotors (char toEncrypt, char (&passedRotorOne)[26], char (&passedRotorTwo)[26])
{
char character = toEncrypt;
character = encryptOneRotor(character, passedRotorOne, true);
character = encryptOneRotor(character, passedRotorTwo, false);
return character;
}

char decryptTwoRotors (char toDecrypt, char (&passedRotorOne)[26], char (&passedRotorTwo)[26])
{
char character = toDecrypt;
character = decryptOneRotor(character, passedRotorTwo, false);
character = decryptOneRotor(character, passedRotorOne, true);
return character;
}


void rotateRotor(char (&passedRotor)[26], bool forward)
{
char firstCharacter;
if (forward == true)
{
firstCharacter = passedRotor[0];
for (int i = 0; i < 26; i++)
{
passedRotor[i] = passedRotor[i + 1];
}

passedRotor[25] = firstCharacter;
}
else
{
firstCharacter = passedRotor[25];
for (int i = 25; i > 0; i--)
{
passedRotor[i] = passedRotor[i - 1];
}

passedRotor[0] = firstCharacter;
}
}
Topic archived. No new replies allowed.