Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,540 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,531 people online right now. Registration is fast and FREE... Join Now!




C++ Cipher code

 
Reply to this topicStart new topic

C++ Cipher code, I get the filter to loop back to a from z

C++beginner
1 Dec, 2007 - 05:33 PM
Post #1

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 5


My Contributions
I'm taking a C++ class and doing this program where i can find how to setup the filter so that i can get my cipher code to loop back form z to a or Z to A, when I'm encoding and my decoding is all wrong too. Can anyone help?


CODE
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;


class coder                                                                    
{
     private:                                                                  
          string encoded;
          string decoded;
          int dlen;
          int shiftk;
     public:                                                                  
          void encode()        
          {
               getline(cin, decoded, '~');
               cipk();            //Gets shift key from user
               int dlen = decoded.length();            //Sets the lenght from the string
               for (int j=0; j<decoded.length(); j++)
                    if ((decoded.at(j) >= 'a' && decoded.at(j) <= 'z') || (decoded.at(j) >= 'A' && decoded.at(j) <= 'Z'))            //Changes the string to characters.
                        //decoded.at(j) = decoded.at(j) + shiftk;            //Adds shift key to the character
                        if (decoded.at(j) + shiftk >= 'a')            //Loops equation back to "a"
                        decoded.at(j) = ('a' + shiftk++);
                        else if (decoded.at(j) + shiftk >= 'A')            //Loops equation back to "A"
                        decoded.at(j) = ('A' + shiftk++);
                    //else if (decoded.at(j) > 'Z')
               cout << "\nYour encoded string is: \n" << decoded << endl;        //Outputs encoded string
          }
          void decode()
          {
               getline (cin, encoded, '~');
               cipk();            //Gets shift key from user
               int elen = encoded.length();            //Sets the length from the string
               for (int j=0; j<encoded.length(); j++)
                    if ((encoded.at(j) >= 'a' && encoded.at(j) <= 'z') || (encoded.at(j) >= 'A' && encoded.at(j) <= 'Z'))
                        //encoded.at(j) = encoded.at(j) - shiftk;
                            if (encoded.at(j) - shiftk <= 'z')            //Loops equation back to "z"
                        encoded.at(j) = ('z' - --shiftk);
                    else if (encoded.at(j) - shiftk <= 'Z')            //Loops equation back to "Z"
                        encoded.at(j) = ('Z' - --shiftk);
               cout << "\nYour decoded string is: \n"<< encoded << endl;
          }
          void cipk()            //Cipher key
          {
                do
                {
                    cout<<"\nWhat integer shift key would you like to use? (0-20): ";
                    cin>>shiftk;
                }
                while (shiftk >= 21 || shiftk <= -1);
          }
};

int main()
{
     coder shift;
     string opt;
     do                                                                        
     {
          cout << "Welcome to the Ceasar Encoder/Decoder!" << endl;
         cout << "Would you like to encode, decode, or quit?  "; cin >> opt;
         if(opt == "encode" || opt == "Encode")
         {
             cout << "\nWhat string would you like to encode?" << endl;
             cout << "(Please end your string with a ~ symbol then press enter)" << endl;
             shift.encode();        //Calls encoder
             do
             {
             cout << "\nWould you like to encode or decode another string (y/n): "; cin >> opt;
             cout << "\n";
             if (opt == "y" || opt == "Y")
             {
                
                 continue;
             }
             else if (opt == "n" || opt == "N")
             {
                 cout << "Thank you for using this program!";
                 return 0;
             }
             else
                 cout << "That's not a valid choice!";
             }while (opt != "y" && opt != "Y" && opt != "n" && opt != "N");
         }
         else if(opt == "decode" || opt == "Decode")
         {
             cout << "\nWhat string would you like to decode?" << endl;
               cout << "(Please end your string with a ~ symbol then press enter)" << endl;
             shift.decode();            //Calls decoder
             do
             {
             cout << "\nWould you like to encode or decode another string (y/n): "; cin >> opt;
             cout << "\n";
             if (opt == "y" || opt == "Y")
             {
                
                 continue;
             }
             else if (opt == "n" || opt == "N")
             {
                 cout << "Thank you for using this program!";
                 return 0;
             }
             else
                 cout << "That's not a valid choice!";
             }while (opt != "y" && opt != "Y" && opt != "n" && opt != "N");
         }
         else if( opt == "quit" || opt == "Quit")
         {
             cout << "Thank you for using this program!\n" << endl;
             return 0;
         }
         else
         {
             cout << "That's not a valid choice!" << endl;
         }
     }while(opt != "n" && opt != "N");
    
     system ("PAUSE");
     return 0;
}


Mod Edit: Added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: C++ Cipher Code
1 Dec, 2007 - 11:57 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hello C++beginner,

Here is a fix for your shifting problem. I only took into account lower case lettering, but I will leave it up to you to modify for upper case. Just be careful because there are characters in between the letter sets.

CODE

void encode()        
{
     getline(cin, decoded, '~');
     cipk();            //Gets shift key from user
     int dlen = decoded.length();            //Sets the lenght from the string
     for (int j=0; j<decoded.length(); j++) {
          // Check if character is between lowercase "a" and "z"
          if (decoded.at(j) >= 'a' && decoded.at(j) <= 'z') {
               // Apply the shift and check it against "z"
               int shifted = decoded.at(j) + shiftk;

               // Greater than "z", so subtract 26 to take it to "a"
               // Then apply the shift
               if (shifted > 'z') {
                    decoded.at(j) = ((decoded.at(j) - 26) + shiftk);
               }
               else {
                    // Shift can happen without intervention
                    decoded.at(j) = shifted;
               }
          }
     }
     cout << "\nYour encoded string is: \n" << decoded << endl;        
}

void decode()
{
     getline (cin, encoded, '~');
     cipk();            //Gets shift key from user
     int elen = encoded.length();            //Sets the length from the string
     for (int j=0; j<encoded.length(); j++) {
          // Check if character is between lowercase "a" and "z"
          if (encoded.at(j) >= 'a' && encoded.at(j) <= 'z') {
               // Subtract the shift and check if it is a number less than "a"
               int shifted = encoded.at(j) - shiftk;

               // Yes it is less than "a", so add 26 to take it to "z"
               // Then apply the shift
               if (shifted < 'a') {
                    encoded.at(j) = ((encoded.at(j) + 26) - shiftk);
               }
               else {
                    // Shift is fine, so just shift.
                    encoded.at(j) = shifted;
               }
          }
     }
     cout << "\nYour decoded string is: \n"<< encoded << endl;
}


But this should get you moving in the right direction with the shifting cipher. Hope it helps you out!

Enjoy!

"At DIC we be caesar cipher shifting code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

C++beginner
RE: C++ Cipher Code
2 Dec, 2007 - 09:38 AM
Post #3

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 5


My Contributions
Thank you so much Martyr2!!!!
User is offlineProfile CardPM
+Quote Post

C++beginner
RE: C++ Cipher Code
5 Dec, 2007 - 11:11 PM
Post #4

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 5


My Contributions
This was what i did i works perfectly. Thanks so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~


/*****************************************************
*Program Name: Caesar Encoding
*Created Date: 25 November, 2007
*Created By: C++beginner
*Purpose: To encode and decode a message for secret missions
*Acknowledgements: dreamincode.com
********************************************************/
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

/*****************************************************************
*Function Name: class code
*Parameters: None
*Return Value: string
*Purpose: To encode and decode a message for secret missions
*****************************************************************/
class coder
{
private:
string encoded;
string decoded;
int dlen;
int shiftk;
public:

void encode()
{
getline(cin, decoded, '~');
cipk(); //Gets shift key from user
int dlen = decoded.length(); //Sets the lenght from the string
for (int j=0; j<decoded.length(); j++) { // Check if character is between lowercase "a" and "z"
if (decoded.at(j) >= 'a' && decoded.at(j) <= 'z') { // Apply the shift and check it against "z"
int shifted = decoded.at(j) + shiftk;
if (shifted > 'z') { // Greater than "z", so subtract 26 to take it to "a"
decoded.at(j) = ((decoded.at(j) - 26) + shiftk); // Then apply the shift
}
else { // Shift can happen without intervention
decoded.at(j) = shifted;
}
}
if (decoded.at(j) >= 'A' && decoded.at(j) <= 'Z') { // Apply the shift and check it against "z"
int shifted = decoded.at(j) + shiftk;
if (shifted > 'Z') { // Greater than "z", so subtract 26 to take it to "a"
decoded.at(j) = ((decoded.at(j) - 26) + shiftk); // Then apply the shift
}
else { // Shift can happen without intervention
decoded.at(j) = shifted;
}
}
}
cout << "\nYour encoded string is: \n" << decoded << endl;
}

void decode()
{
getline (cin, encoded, '~');
cipk(); //Gets shift key from user
int elen = encoded.length(); //Sets the length from the string
for (int j=0; j<encoded.length(); j++) {
if (encoded.at(j) >= 'a' && encoded.at(j) <= 'z') { // Check if character is between lowercase "a" and "z"

int shifted = encoded.at(j) - shiftk; // Subtract the shift and check if it is a number less than "a" or "A"

if (shifted < 'a') { // Yes it is less than "a", so add 26 to take it to "z"
encoded.at(j) = ((encoded.at(j) + 26) - shiftk); // Then apply the shift
}
else {
encoded.at(j) = shifted; // Shift is fine, so just shift.
}
}

if (encoded.at(j) >= 'A' && encoded.at(j) <= 'Z') { // Check if character is between lowercase "a" and "z"

int shifted = encoded.at(j) - shiftk; // Subtract the shift and check if it is a number less than "a" or "A"

if (shifted < 'A') { // Yes it is less than "a", so add 26 to take it to "z"
encoded.at(j) = ((encoded.at(j) + 26) - shiftk); // Then apply the shift
}
else {
encoded.at(j) = shifted; // Shift is fine, so just shift.
}
}
}
cout << "\nYour decoded string is: \n"<< encoded << endl;
}
void cipk() //Cipher key
{
do
{
cout<<"\nWhat integer shift key would you like to use? (0-20): ";
cin>>shiftk;
}
while (shiftk >= 21 || shiftk <= -1);
}
};
/*****************************************************************
*Function Name: main()
*Parameters: None
*Return Value: int
*Purpose: Encoding and Decoding strings using cipher shifting
*****************************************************************/
int main()
{
coder shift;
string opt;
do
{
cout << "Welcome to the Ceasar Encoder/Decoder!" << endl;
cout << "Would you like to encode, decode, or quit? "; cin >> opt;
if(opt == "encode" || opt == "Encode"){ //allow user to enter either encode or Encode

cout << "\nWhat string would you like to encode?" << endl;
cout << "(Please end your string with a ~ symbol then press enter)" << endl;
shift.encode(); //Calls encoder
do
{
cout << "\nWould you like to encode or decode another string (y/n): "; cin >> opt;
cout << "\n";
if (opt == "y" || opt == "Y") //allow user to either enter y or Y
{
continue;
}
else if (opt == "n" || opt == "N") //allow user to either enter n or N
{
cout << "Thank you for using this program!";
return 0;
}
else
cout << "That's not a valid choice!";
}while (opt != "y" && opt != "Y" && opt != "n" && opt != "N"); //sets choose so that it's only "y, Y, n, or N"
}
else if(opt == "decode" || opt == "Decode"){ //allow user to choose decode or Decode

cout << "\nWhat string would you like to decode?" << endl;
cout << "(Please end your string with a ~ symbol then press enter)" << endl;
shift.decode(); //Calls decoder

do {
cout << "\nWould you like to encode or decode another string (y/n): "; cin >> opt;
cout << "\n";

if (opt == "y" || opt == "Y"){ //if user enters y or Y, game loops back to the beginning

continue;
}
else if (opt == "n" || opt == "N"){ //if user enters n or N, game will end

cout << "Thank you for using this program!";
return 0;
}
else
cout << "That's not a valid choice!";
}while (opt != "y" && opt != "Y" && opt != "n" && opt != "N"); //allow users these options
}
else if( opt == "quit" || opt == "Quit"){ //allow user to quit game

cout << "Thank you for using this program!\n" << endl;
return 0;
}
else{

cout << "That's not a valid choice!" << endl;
}
}while(opt != "n" && opt != "N"); //sets it for n or N

system ("PAUSE");
return 0;
}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month