The current code at the bottom works but I can't combine the if statements without shifting the ascii values to a position that I don't want them to be. The encryption is supposed to be only alphabet values. Capital and lowercase z's are supposed to loop around to an A. I have a teacher but she doesn't know so I would be grateful for any help. Thanks <3
This doesn't work...
if (sentence[i] == 'z' || 'Z')
{
sentence[i] = sentence[i] - 26;
}
And this doesn't work
if (sentence[i] == 'z' || sentence[i] == 'Z')
{
sentence[i] = sentence[i] - 26;
}
This works.
if (sentence[i] == 'z')
{
sentence[i] = sentence[i] - 26;
}
if (sentence[i] == 'Z')
{
sentence[i] = sentence[i] - 26;
}
Full code.
#include <iostream>
#include <string>
using namespace std;
class EncryptionClass
{
string sentence;
public:
//constructors
EncryptionClass(string sentence)
{setString(sentence);}
EncryptionClass()
{sentence = "";}
//get and set
string getString()
{return sentence;}
void setString(string sentence)
{this-> sentence = sentence;}
//encrypt
void encryptString()
{
for(int i = 0; i < sentence.length(); i++)
{
if (isalpha(sentence[i]))
{
if (sentence[i] == 'z')
{
sentence[i] = sentence[i] - 26;
}
if (sentence[i] == 'Z')
{
sentence[i] = sentence[i] - 26;
}
sentence[i] = sentence[i] + 1;
}
}
}
};
int main()
{
string sentence;
cout << "Enter a sentence to be encrypted. ";
getline(cin, sentence);
cout << endl;
EncryptionClass sentence1(sentence);
cout << "Unencrypted sentence." << endl;
cout << sentence1.getString() << endl << endl;
sentence1.encryptString();
cout << "Encrypted sentence." << endl;
cout << sentence1.getString() << endl;
cin.get();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire