C++ Caesar cipher - follow-upImplementation, Ceaser Cipher in C++Learning Ruby Caesar CipherCaesar Cipher...

What is better: yes / no radio, or simple checkbox?

Can I negotiate a patent idea for a raise, under French law?

Trocar background-image com delay via jQuery

Will expression retain the same definition if particle is changed?

How do you make a gun that shoots melee weapons and/or swords?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Smooth vector fields on a surface modulo diffeomorphisms

Use Mercury as quenching liquid for swords?

What can I do if someone tampers with my SSH public key?

How to install round brake pads

Writing text next to a table

I reported the illegal activity of my boss to his boss. My boss found out. Now I am being punished. What should I do?

ESPP--any reason not to go all in?

Converting from "matrix" data into "coordinate" data

Is there a logarithm base for which the logarithm becomes an identity function?

How do I raise a figure (placed with wrapfig) to be flush with the top of a paragraph?

Idiom for feeling after taking risk and someone else being rewarded

Strange opamp's output impedance in spice

Why is there an extra space when I type "ls" on the Desktop?

Is there stress on two letters on the word стоят

Why aren't there more Gauls like Obelix?

Are E natural minor and B harmonic minor related?

What does *dead* mean in *What do you mean, dead?*?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?



C++ Caesar cipher - follow-up


Implementation, Ceaser Cipher in C++Learning Ruby Caesar CipherCaesar Cipher encryption/decryptionShifting chars (Caesar Cipher)Simple Caesar Cipher FunctionSimple Caesar Cipher C++Caesar cipher in CBasic Caesar Cipher CodeCaesar Cipher in HaskellCaesar cipher encryptionCaesar Cipher improvement













2












$begingroup$


I asked another question the other day and decided to rewrite the code with some of the suggestions given and add functionality (instead of reading from a console I'm reading directly from a file) the code works.



Looking at it, I think the nested for loop on the encode function doesn't look that good. I tried using std::transform like the comment on the code. It worked when I used the console just to read a line, now with vectors couldn't make it work.



// Ceaser Cipher implementation

#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <vector>


std::vector<std::string> encode(const std::vector<std::string> &str, int shift)
{
std::vector<std::string> tempMsg;

for (std::string lines : str)
{
// std::transform(lines.cbegin(), lines.cend(), std::back_inserter(tempMsg), [&](char ch) -> char
for (char &ch : lines)
{
if (ch == 'z' || ch == 'Z')
{
ch -= 25;
}
else if (isspace(ch)) {
ch = ' ';
}
else
{
ch += shift;
}
}
tempMsg.push_back(lines);
}

return tempMsg;
}

std::vector<std::string> decode(const std::vector<std::string> &str, int shift)
{
return encode(str, -1 * shift);
}


int main(int argc, char *argv[])
{
int choice;
std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
std::cin >> choice;


int key;
std::cout << "Enter desired shift: ";
std::cin >> key;


std::ifstream inFile("testfile.txt");
if (!(inFile.is_open()))
{
std::cout << "There was a problem with the file!";
std::exit(EXIT_FAILURE); // ExitProcess() if windows especific.
}

std::vector<std::string> finalResult;

std::string line;
std::vector<std::string> lines;


while (std::getline(inFile, line))
{
lines.push_back(line);
}
inFile.close();

if (choice == 1)
{
auto result = encode(lines, key);
finalResult = result;
}

else if (choice == 2)
{
auto result = decode(lines, key);
finalResult = result;
}
else
{
std::cout << "Wrong option, exiting!";
std::exit(EXIT_FAILURE);
}


std::ofstream outFile("testfile.txt");
for (auto i = finalResult.begin(); i != finalResult.end(); ++i)
{
outFile << *i << 'n';
}
outFile.close();

std::exit(EXIT_SUCCESS);
}









share|improve this question









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$

















    2












    $begingroup$


    I asked another question the other day and decided to rewrite the code with some of the suggestions given and add functionality (instead of reading from a console I'm reading directly from a file) the code works.



    Looking at it, I think the nested for loop on the encode function doesn't look that good. I tried using std::transform like the comment on the code. It worked when I used the console just to read a line, now with vectors couldn't make it work.



    // Ceaser Cipher implementation

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <fstream>
    #include <vector>


    std::vector<std::string> encode(const std::vector<std::string> &str, int shift)
    {
    std::vector<std::string> tempMsg;

    for (std::string lines : str)
    {
    // std::transform(lines.cbegin(), lines.cend(), std::back_inserter(tempMsg), [&](char ch) -> char
    for (char &ch : lines)
    {
    if (ch == 'z' || ch == 'Z')
    {
    ch -= 25;
    }
    else if (isspace(ch)) {
    ch = ' ';
    }
    else
    {
    ch += shift;
    }
    }
    tempMsg.push_back(lines);
    }

    return tempMsg;
    }

    std::vector<std::string> decode(const std::vector<std::string> &str, int shift)
    {
    return encode(str, -1 * shift);
    }


    int main(int argc, char *argv[])
    {
    int choice;
    std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
    std::cin >> choice;


    int key;
    std::cout << "Enter desired shift: ";
    std::cin >> key;


    std::ifstream inFile("testfile.txt");
    if (!(inFile.is_open()))
    {
    std::cout << "There was a problem with the file!";
    std::exit(EXIT_FAILURE); // ExitProcess() if windows especific.
    }

    std::vector<std::string> finalResult;

    std::string line;
    std::vector<std::string> lines;


    while (std::getline(inFile, line))
    {
    lines.push_back(line);
    }
    inFile.close();

    if (choice == 1)
    {
    auto result = encode(lines, key);
    finalResult = result;
    }

    else if (choice == 2)
    {
    auto result = decode(lines, key);
    finalResult = result;
    }
    else
    {
    std::cout << "Wrong option, exiting!";
    std::exit(EXIT_FAILURE);
    }


    std::ofstream outFile("testfile.txt");
    for (auto i = finalResult.begin(); i != finalResult.end(); ++i)
    {
    outFile << *i << 'n';
    }
    outFile.close();

    std::exit(EXIT_SUCCESS);
    }









    share|improve this question









    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$















      2












      2








      2





      $begingroup$


      I asked another question the other day and decided to rewrite the code with some of the suggestions given and add functionality (instead of reading from a console I'm reading directly from a file) the code works.



      Looking at it, I think the nested for loop on the encode function doesn't look that good. I tried using std::transform like the comment on the code. It worked when I used the console just to read a line, now with vectors couldn't make it work.



      // Ceaser Cipher implementation

      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <fstream>
      #include <vector>


      std::vector<std::string> encode(const std::vector<std::string> &str, int shift)
      {
      std::vector<std::string> tempMsg;

      for (std::string lines : str)
      {
      // std::transform(lines.cbegin(), lines.cend(), std::back_inserter(tempMsg), [&](char ch) -> char
      for (char &ch : lines)
      {
      if (ch == 'z' || ch == 'Z')
      {
      ch -= 25;
      }
      else if (isspace(ch)) {
      ch = ' ';
      }
      else
      {
      ch += shift;
      }
      }
      tempMsg.push_back(lines);
      }

      return tempMsg;
      }

      std::vector<std::string> decode(const std::vector<std::string> &str, int shift)
      {
      return encode(str, -1 * shift);
      }


      int main(int argc, char *argv[])
      {
      int choice;
      std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
      std::cin >> choice;


      int key;
      std::cout << "Enter desired shift: ";
      std::cin >> key;


      std::ifstream inFile("testfile.txt");
      if (!(inFile.is_open()))
      {
      std::cout << "There was a problem with the file!";
      std::exit(EXIT_FAILURE); // ExitProcess() if windows especific.
      }

      std::vector<std::string> finalResult;

      std::string line;
      std::vector<std::string> lines;


      while (std::getline(inFile, line))
      {
      lines.push_back(line);
      }
      inFile.close();

      if (choice == 1)
      {
      auto result = encode(lines, key);
      finalResult = result;
      }

      else if (choice == 2)
      {
      auto result = decode(lines, key);
      finalResult = result;
      }
      else
      {
      std::cout << "Wrong option, exiting!";
      std::exit(EXIT_FAILURE);
      }


      std::ofstream outFile("testfile.txt");
      for (auto i = finalResult.begin(); i != finalResult.end(); ++i)
      {
      outFile << *i << 'n';
      }
      outFile.close();

      std::exit(EXIT_SUCCESS);
      }









      share|improve this question









      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 asked another question the other day and decided to rewrite the code with some of the suggestions given and add functionality (instead of reading from a console I'm reading directly from a file) the code works.



      Looking at it, I think the nested for loop on the encode function doesn't look that good. I tried using std::transform like the comment on the code. It worked when I used the console just to read a line, now with vectors couldn't make it work.



      // Ceaser Cipher implementation

      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <fstream>
      #include <vector>


      std::vector<std::string> encode(const std::vector<std::string> &str, int shift)
      {
      std::vector<std::string> tempMsg;

      for (std::string lines : str)
      {
      // std::transform(lines.cbegin(), lines.cend(), std::back_inserter(tempMsg), [&](char ch) -> char
      for (char &ch : lines)
      {
      if (ch == 'z' || ch == 'Z')
      {
      ch -= 25;
      }
      else if (isspace(ch)) {
      ch = ' ';
      }
      else
      {
      ch += shift;
      }
      }
      tempMsg.push_back(lines);
      }

      return tempMsg;
      }

      std::vector<std::string> decode(const std::vector<std::string> &str, int shift)
      {
      return encode(str, -1 * shift);
      }


      int main(int argc, char *argv[])
      {
      int choice;
      std::cout << "What do you want to do? 1.Encrypt, 2.Decrypt: ";
      std::cin >> choice;


      int key;
      std::cout << "Enter desired shift: ";
      std::cin >> key;


      std::ifstream inFile("testfile.txt");
      if (!(inFile.is_open()))
      {
      std::cout << "There was a problem with the file!";
      std::exit(EXIT_FAILURE); // ExitProcess() if windows especific.
      }

      std::vector<std::string> finalResult;

      std::string line;
      std::vector<std::string> lines;


      while (std::getline(inFile, line))
      {
      lines.push_back(line);
      }
      inFile.close();

      if (choice == 1)
      {
      auto result = encode(lines, key);
      finalResult = result;
      }

      else if (choice == 2)
      {
      auto result = decode(lines, key);
      finalResult = result;
      }
      else
      {
      std::cout << "Wrong option, exiting!";
      std::exit(EXIT_FAILURE);
      }


      std::ofstream outFile("testfile.txt");
      for (auto i = finalResult.begin(); i != finalResult.end(); ++i)
      {
      outFile << *i << 'n';
      }
      outFile.close();

      std::exit(EXIT_SUCCESS);
      }






      c++ caesar-cipher






      share|improve this question









      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|improve this question









      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|improve this question




      share|improve this question








      edited 3 mins ago









      Jamal

      30.4k11120227




      30.4k11120227






      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 6 hours ago









      ExzlantttExzlanttt

      313




      313




      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%2f215115%2fc-caesar-cipher-follow-up%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%2f215115%2fc-caesar-cipher-follow-up%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

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...