0

这是我尝试加密“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;
}
4

2 回答 2

3

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

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

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

或者摆脱char数组并使用std::string.

于 2015-08-29T17:47:55.197 回答
0

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

#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;
}
于 2015-08-29T21:30:19.657 回答