C++ Caesar cipher - follow-up












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 4 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

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар