C++凯撒密码程序执行错误的字符

c++ caesar cipher program executes wrong character

这是我尝试加密 "This is a test sentence!" 时得到的结果:"Ymnxp���p�����t�������"

我之前测试过我的加密部分,它运行良好。

谁能告诉我我做错了什么?

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"

using namespace std;


void displayUsage(){
    // Displays how to use this program
    // TODO
    cout << "Instruction: \n use -r to give rotation number \n use -f to give file name" <<endl;
}


int main(int argc, char **argv){

  string text;
  char* input_file_name;
  int rotation;
  bool have_rotation = false;
  bool have_input_file_name = false;


    // process command-line arguement
    int opt = 0;
    extern char *optarg;
    static const char* opt_string = "r:f:";
    opt = getopt( argc, argv, opt_string);
    while(opt != -1) // While there are parameters to parse, do so
    {  
        switch(opt)
        {
            case 'r':
                have_rotation = true;
                rotation = atoi(optarg);
                break;
            case 'f':
                have_input_file_name = true;
                input_file_name = optarg;
                break;
            default:
              displayUsage();
              return 1;
        }
        opt = getopt( argc, argv, opt_string);  // Pull the next parameter, or 0 if none.
    }

    if(!have_rotation)
    {
      displayUsage();
      return 0;
    }

    if(have_rotation)
    {
      if(have_input_file_name)
      {     
        ifstream file(input_file_name);
        string text2, temp;
        while(!file.eof())
        {
          getline(file, temp);
          text2 += temp;
          text2 += "\n";
        }
        text = text2[text2.size()-2];
      }
      else
      {
      cout <<"Enter text:"<<endl;
      cin >> text;
     }
    }

    char cipher[text.size()];

    for(int i=0; i<text.size(); i++)
    {
      cipher[i] = text[i];
      if(islower(cipher[i]))
      {
        cipher[i] = (cipher[i] - 'a' + rotation)%26 + 'a';
      }
      else if(isupper(cipher[i]))
      {
        cipher[i] = (cipher[i] - 'A' + rotation)%26 + 'A';
      }
    }

    cout <<cipher<<endl;

  return 0;
}

我猜这个错误是因为你没有用 '\0' 终止你的 cipher 数组。

打印函数将处理数组中的字符(可能超出数组),直到找到“\0”字符。

您的数组应该大一个以说明这个终止字符。

或者删除 char 数组并使用 std::string

如果我手动输入句子,我可以获得我的代码 运行。如果我输入文本文件,它不会打印出任何内容。

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "unistd.h"

using namespace std;


void displayUsage(){
// Displays how to use this program
// TODO
cout << "Instruction: \n use -r to give rotation number \n use -f to give    file name" <<endl;
}

char caesar(char c, int r)
{
  if(isalpha(c))
  {
    if(islower(c))
    {
      c = (((c-97)+r)%26)+97; // 97 is a value of 'a'
    }
    else if(isupper(c))
    {
      c = (((c-65)+r)%26)+65; // 65 is a value of 'A'
    }
  }
  return c;
}


int main(int argc, char **argv){

  string text;
  char* input_file_name;
  int rotation;
  bool have_rotation = false;
  bool have_input_file_name = false;


    // process command-line arguement
    int opt = 0;
    extern char *optarg;
    static const char* opt_string = "r:f:";
    opt = getopt( argc, argv, opt_string);
    while(opt != -1) // While there are parameters to parse, do so
    {  
        switch(opt)
        {
            case 'r':
                have_rotation = true;
                rotation = atoi(optarg);
                break;
            case 'f':
                have_input_file_name = true;
                input_file_name = optarg;
                break;
            default:
                displayUsage();
                return 1;
        }
        opt = getopt( argc, argv, opt_string);  // Pull the next parameter, or 0 if none.
    }

    if(!have_rotation)
    {
      displayUsage();
      return 0;
    }

    if(have_rotation)
    {
      if(have_input_file_name)
      {     
        ifstream file(input_file_name);
        string text2, temp;
        while(!file.eof())
       {
          getline(file, temp);
          text2 += temp + "\n";
        }
        text = text2[text2.size()-2];
       }
      else
      {
      cout <<"Enter text:"<<endl;
      getline(cin, text);
      }
    }

      string output = "";
      for(int i = 0; i < text.size(); i++)
      {
        output += caesar(text[i],rotation);
      }
      cout<<output<<endl;


  return 0;
}