Afternoon all, Here for homework of course. I am to take an input of something like:
my name is bob
and output an encrypted form of it. With that information, I am to input the encrypted version and decrypt it. As I am sure you will be able to tell from my code, I am very new to this. The methods I am using are those I am familiar with.
The issue atm is that my getline is not working inside the loops. I tries it without the loops to no avail there either.
Thanks ahead of time for any input. Of course since this is homework, I do not want the answer, just a push in the right direction.
int w;
cout << "Welcome to the encryption/decryption program" << endl;
do
{
cout << "Enter [1] for encryption" << endl;
cout << "Enter [2] for decryption" << endl;
cout << "Enter [0] to quit" << endl;
cin >> w;
if (w == 1)
{
char datae[127];
cout << "Enter the data to be encrypted: "<< endl;
cin.getline(datae,127);
cout << "The data to be encrypted is: " << endl;
cout << datae << endl;
string enc = encrypt(datae);
cout << "your encrypted text is now: " << endl;
cout << enc << endl;
}
else if(w == 2)
{
char datad[127];
cout << "Enter the data to be decrypted: "<< endl ;
cin.getline(datad,127);
cout << "The data to be dencrypted is: " << endl;
cout << datad << endl << endl;
string dec = decrypt(datad);
cout << "your decrypted text is now: " << endl;
cout << dec << endl;
}
else if ((w < 0) || w > 3 || (cin.fail() == true))
{
cout << "The input must be between 0 and 3 " << endl;
cin.clear();
cin.ignore(50,'\n');
continue;
}
}
while (w != 0);
cin.ignore (2);
return(0);
}//end
string encrypt(char dataE[]) // encrypt
{
int alpha1 [127];
int beta1 [127];
int theta1 [127];
char mapE[127];
cout << "Enter the encryption string: " << endl << endl;
cout << "Entry must me less than 128 lower case " << endl;
cout << "alphabetical characters " << endl << endl;
cin >> mapE;
cout << "The data key is: " << endl << endl;
cout << mapE << endl << endl;
int maplength = strlen(mapE);
int datalength = strlen(dataE);
string str1;
char c;
for (int i = 0; i < datalength; i++)
{
int x = dataE[i] - 'a';
alpha1 [i] = x;
}
for (int k = 0; k < maplength; k++)
{
int y = mapE[k] - 'a';
beta1 [k] = y;
}
for (int i = 0; i < datalength; i++)
{
if (alpha1 [i] == -65)
{
theta1 [i] = alpha1[i];
}
else
{
theta1 [i] = (alpha1[i] + beta1 [i % maplength]) % 26 + 'a';
}
int p = theta1[i];
if (p == -65)
{
c =' ';
}
else
{
c = static_cast<char>(p);
}
str1 = str1 + c;
}
return str1;
}
string decrypt(char dataD[]) // decrypt
{
int alpha2 [127];
int beta2 [127];
int theta2 [127];
char mapD[127];
cout << "Enter the decryption string: " << endl << endl;
cout << "Entry must me less than 128 lower case " << endl;
cout << "alphabetical characters " << endl << endl;
cin >> mapD;
cout << "The data key is: " << endl << endl;
cout << mapD << endl << endl;
int maplengthD = strlen(mapD);
int datalengthD = strlen(dataD);
string str2;
char c;
for (int i = 0; i < datalengthD; i++)
{
int x = dataD[i] - 'a';
alpha2 [i] = x;
}
for (int k = 0; k < maplengthD; k++)
{
int y = mapD[k] - 'a';
beta2 [k] = 26 - y;
}
for (int i = 0; i < datalengthD; i++)
{
if (alpha2 [i] == -65)
{
theta2 [i] = alpha2[i];
}
else
{
theta2 [i] = (alpha2[i] + beta2 [i % maplengthD]) % 26 + 'a';
}
int p = theta2[i];
if (p == -65)
{
c =' ';
}
else
{
c = static_cast<char>(p);
}
str2 = str2 + c;
}
return str2;
}
The new line character ends the >> extraction. Getline() reads until it finds a newline and extracts it. The first example fails because there is a newline character in the buffer when getline() is called.