Implementation, Ceaser Cipher in C++Caesar cipher implementationCaesar Cipher implementation - Odin...

Is there a frame of reference in which I was born before I was conceived?

School performs periodic password audits. Is my password compromised?

Correct physics behind the colors on CD (compact disc)?

Is every open circuit a capacitor?

How can I handle a player who pre-plans arguments about my rulings on RAW?

Caulking a corner instead of taping with joint compound?

Do AL rules let me pick different starting equipment?

Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?

When do _WA_Sys_ statistics Get Updated?

Where is the fallacy here?

The need of reserving one's ability in job interviews

Lock enemy's y-axis when using Vector3.MoveTowards to follow the player

Is there a limit on the maximum number of future jobs queued in an org?

Why are special aircraft used for the carriers in the United States Navy?

Practical reasons to have both a large police force and bounty hunting network?

Quitting employee has privileged access to critical information

I can't die. Who am I?

If there are any 3nion, 5nion, 7nion, 9nion, 10nion, etc.

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

PTIJ: Aharon, King of Egypt

What could be a means to defeat a childrens’ nightmare?

How to fix my table, centering of columns

Can we carry rice to Japan?

Create chunks from an array



Implementation, Ceaser Cipher in C++


Caesar cipher implementationCaesar Cipher implementation - Odin ProjectEncode a string using Caesar's CipherShifting chars (Caesar Cipher)Simple Caesar Cipher C++Simple Affine cipherCaesar Cipher implementation in JavaCaesar Cipher MVC implementationCaesar Cipher in HaskellCaesar cipher encryption













1












$begingroup$


I'm working on learning C++ coming from python. I'm doing some projects to help me out understanding the language a bit more.



My code works I just want to know if there is anything that I can improve on it or best practices in C++, maybe something that I should have done differently.



// Ceaser Cipher implementation

#include <iostream>
#include <string>




char shiftF(char ch, int shift, const char type)
{
if (type == 'e')
{
return ch == 'z' || ch == 'Z' ? ch - 25 : ch + shift;
}
else if (type == 'd')
{
return ch == 'z' || ch == 'Z' ? ch - 25 : ch - shift;
}
}


std::string encode(std::string str, int shift, char type)
{
std::string tempMsg;
for (auto ch : str)
{
if (isalpha(ch))
{
tempMsg += shiftF(ch, shift, type);
}
else if (isspace(ch))
{
// There is probably a better way to do this.
tempMsg += " ";
}
else if (isalnum(ch))
{
continue;
}
}
return tempMsg;
}


int main(int argc, char *argv[])
{
int choice;
std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
std::cin >> choice;
std::string result;
const char dec = 'd';
const char enc = 'e';

if (choice == 1)
{
int key;
std::cout << "Enter encryption shift: ";
std::cin >> key;

std::string msg;
std::cout << "Enter a message: ";

// This doesn't work for some reason?
// std::cin.clear();
// std::cin.sync();

std::cin.ignore();
std::getline(std::cin, msg);

result = encode(msg, key, enc);
}

else if (choice == 2)
{
int key;
std::cout << "Enter decryption shift: ";
std::cin >> key;

std::string msg;
std::cout << "Enter a message: ";

std::cin.ignore();
std::getline(std::cin, msg);

result = encode(msg, key, dec);
}
else
{
std::cout << "Wrong option, exiting!";
}

std::cout << "Message encoded: " << result;
}








share







New contributor




Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    I'm working on learning C++ coming from python. I'm doing some projects to help me out understanding the language a bit more.



    My code works I just want to know if there is anything that I can improve on it or best practices in C++, maybe something that I should have done differently.



    // Ceaser Cipher implementation

    #include <iostream>
    #include <string>




    char shiftF(char ch, int shift, const char type)
    {
    if (type == 'e')
    {
    return ch == 'z' || ch == 'Z' ? ch - 25 : ch + shift;
    }
    else if (type == 'd')
    {
    return ch == 'z' || ch == 'Z' ? ch - 25 : ch - shift;
    }
    }


    std::string encode(std::string str, int shift, char type)
    {
    std::string tempMsg;
    for (auto ch : str)
    {
    if (isalpha(ch))
    {
    tempMsg += shiftF(ch, shift, type);
    }
    else if (isspace(ch))
    {
    // There is probably a better way to do this.
    tempMsg += " ";
    }
    else if (isalnum(ch))
    {
    continue;
    }
    }
    return tempMsg;
    }


    int main(int argc, char *argv[])
    {
    int choice;
    std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
    std::cin >> choice;
    std::string result;
    const char dec = 'd';
    const char enc = 'e';

    if (choice == 1)
    {
    int key;
    std::cout << "Enter encryption shift: ";
    std::cin >> key;

    std::string msg;
    std::cout << "Enter a message: ";

    // This doesn't work for some reason?
    // std::cin.clear();
    // std::cin.sync();

    std::cin.ignore();
    std::getline(std::cin, msg);

    result = encode(msg, key, enc);
    }

    else if (choice == 2)
    {
    int key;
    std::cout << "Enter decryption shift: ";
    std::cin >> key;

    std::string msg;
    std::cout << "Enter a message: ";

    std::cin.ignore();
    std::getline(std::cin, msg);

    result = encode(msg, key, dec);
    }
    else
    {
    std::cout << "Wrong option, exiting!";
    }

    std::cout << "Message encoded: " << result;
    }








    share







    New contributor




    Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      I'm working on learning C++ coming from python. I'm doing some projects to help me out understanding the language a bit more.



      My code works I just want to know if there is anything that I can improve on it or best practices in C++, maybe something that I should have done differently.



      // Ceaser Cipher implementation

      #include <iostream>
      #include <string>




      char shiftF(char ch, int shift, const char type)
      {
      if (type == 'e')
      {
      return ch == 'z' || ch == 'Z' ? ch - 25 : ch + shift;
      }
      else if (type == 'd')
      {
      return ch == 'z' || ch == 'Z' ? ch - 25 : ch - shift;
      }
      }


      std::string encode(std::string str, int shift, char type)
      {
      std::string tempMsg;
      for (auto ch : str)
      {
      if (isalpha(ch))
      {
      tempMsg += shiftF(ch, shift, type);
      }
      else if (isspace(ch))
      {
      // There is probably a better way to do this.
      tempMsg += " ";
      }
      else if (isalnum(ch))
      {
      continue;
      }
      }
      return tempMsg;
      }


      int main(int argc, char *argv[])
      {
      int choice;
      std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
      std::cin >> choice;
      std::string result;
      const char dec = 'd';
      const char enc = 'e';

      if (choice == 1)
      {
      int key;
      std::cout << "Enter encryption shift: ";
      std::cin >> key;

      std::string msg;
      std::cout << "Enter a message: ";

      // This doesn't work for some reason?
      // std::cin.clear();
      // std::cin.sync();

      std::cin.ignore();
      std::getline(std::cin, msg);

      result = encode(msg, key, enc);
      }

      else if (choice == 2)
      {
      int key;
      std::cout << "Enter decryption shift: ";
      std::cin >> key;

      std::string msg;
      std::cout << "Enter a message: ";

      std::cin.ignore();
      std::getline(std::cin, msg);

      result = encode(msg, key, dec);
      }
      else
      {
      std::cout << "Wrong option, exiting!";
      }

      std::cout << "Message encoded: " << result;
      }








      share







      New contributor




      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I'm working on learning C++ coming from python. I'm doing some projects to help me out understanding the language a bit more.



      My code works I just want to know if there is anything that I can improve on it or best practices in C++, maybe something that I should have done differently.



      // Ceaser Cipher implementation

      #include <iostream>
      #include <string>




      char shiftF(char ch, int shift, const char type)
      {
      if (type == 'e')
      {
      return ch == 'z' || ch == 'Z' ? ch - 25 : ch + shift;
      }
      else if (type == 'd')
      {
      return ch == 'z' || ch == 'Z' ? ch - 25 : ch - shift;
      }
      }


      std::string encode(std::string str, int shift, char type)
      {
      std::string tempMsg;
      for (auto ch : str)
      {
      if (isalpha(ch))
      {
      tempMsg += shiftF(ch, shift, type);
      }
      else if (isspace(ch))
      {
      // There is probably a better way to do this.
      tempMsg += " ";
      }
      else if (isalnum(ch))
      {
      continue;
      }
      }
      return tempMsg;
      }


      int main(int argc, char *argv[])
      {
      int choice;
      std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
      std::cin >> choice;
      std::string result;
      const char dec = 'd';
      const char enc = 'e';

      if (choice == 1)
      {
      int key;
      std::cout << "Enter encryption shift: ";
      std::cin >> key;

      std::string msg;
      std::cout << "Enter a message: ";

      // This doesn't work for some reason?
      // std::cin.clear();
      // std::cin.sync();

      std::cin.ignore();
      std::getline(std::cin, msg);

      result = encode(msg, key, enc);
      }

      else if (choice == 2)
      {
      int key;
      std::cout << "Enter decryption shift: ";
      std::cin >> key;

      std::string msg;
      std::cout << "Enter a message: ";

      std::cin.ignore();
      std::getline(std::cin, msg);

      result = encode(msg, key, dec);
      }
      else
      {
      std::cout << "Wrong option, exiting!";
      }

      std::cout << "Message encoded: " << result;
      }






      c++ caesar-cipher





      share







      New contributor




      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      ExzlantttExzlanttt

      6




      6




      New contributor




      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Exzlanttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Exzlanttt is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214879%2fimplementation-ceaser-cipher-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Exzlanttt is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Exzlanttt is a new contributor. Be nice, and check out our Code of Conduct.













          Exzlanttt is a new contributor. Be nice, and check out our Code of Conduct.












          Exzlanttt is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214879%2fimplementation-ceaser-cipher-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Webac Holding Inhaltsverzeichnis Geschichte | Organisationsstruktur | Tochterfirmen |...

          What's the meaning of a knight fighting a snail in medieval book illustrations?What is the meaning of a glove...

          Salamanca Inhaltsverzeichnis Lage und Klima | Bevölkerungsentwicklung | Geschichte | Kultur und...