简体   繁体   English

将字母向前移动3个字母的程序,错误:抛出'std :: out_of_range'实例后终止终止

[英]Program that shifts letters 3 letters forward, error: terminate called after throwing an instance of 'std::out_of_range'

So I'm developing a simple program that converts normal strings using the Caesar Cipher , which just shifts the letters of a string 3 times forward, and to decipher it, just undo it (pretty basic stuff), just as a simple test of my understanding of c++ (I am a beginner) and I am receiving an error not in my IDE (Code::Blocks), but instead in the console: 因此, 我正在开发一个简单的程序,该程序使用Caesar Cipher转换普通字符串 ,该字符串会将字符串的字母向前移动3次,并对其进行解密,只需撤消它(相当基本的东西)即可,就像对我的简单测试一样了解c ++(我是新手),并且收到的错误不是在我的IDE中(代码:: Blocks),而是在控制台中:

terminate called after throwing an instance of 'std::out_of_range'
what():  basic_string::at: __n (which is 1) >= this->size() (which is 0)

Here is my code: 这是我的代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string input;
string conversion;
cin >> input;
for(unsigned int i = 1; i<input.length(); i++){
    if(input.at(i) == 'a' || input.at(i) == 'A'){
        conversion.at(i) = 'D';
    }
     if(input.at(i) == 'b' || input.at(i) == 'B'){
        conversion.at(i) = 'E';
    }
     if(input.at(i) == 'c' || input.at(i) == 'C'){
        conversion.at(i) = 'F';
    }
     if(input.at(i) == 'd' || input.at(i) == 'D'){
        conversion.at(i) = 'G';
    }
     if(input.at(i) == 'e' || input.at(i) == 'E'){
        conversion.at(i) = 'H';
    }
     if(input.at(i) == 'f' || input.at(i) == 'F'){
        conversion.at(i) = 'I';
    }
     if(input.at(i) == 'g' || input.at(i) == 'G'){
        conversion.at(i) = 'J';
    }
     if(input.at(i) == 'h' || input.at(i) == 'H'){
        conversion.at(i) = 'K';
    }
    if(input.at(i) == 'i' || input.at(i) == 'I'){
        conversion.at(i) = 'L';
    }
      if(input.at(i) == 'j' || input.at(i) == 'J'){
        conversion.at(i) = 'M';
    }
      if(input.at(i) == 'k' || input.at(i) == 'K'){
        conversion.at(i) = 'N';
    }
      if(input.at(i) == 'l' || input.at(i) == 'L'){
        conversion.at(i) = 'O';
    }
      if(input.at(i) == 'm' || input.at(i) == 'M'){
        conversion.at(i) = 'P';
    }
      if(input.at(i) == 'n' || input.at(i) == 'N'){
        conversion.at(i) = 'Q';
    }
      if(input.at(i) == 'o' || input.at(i) == 'O'){
        conversion.at(i) = 'R';
    }
      if(input.at(i) == 'p' || input.at(i) == 'P'){
        conversion.at(i) = 'S';
    }
      if(input.at(i) == 'q' || input.at(i) == 'Q'){
        conversion.at(i) = 'T';
    }
      if(input.at(i) == 'r' || input.at(i) == 'R'){
        conversion.at(i) = 'U';
    }
      if(input.at(i) == 's' || input.at(i) == 'S'){
        conversion.at(i) = 'V';
    }
      if(input.at(i) == 't' || input.at(i) == 'T'){
        conversion.at(i) = 'W';
    }
      if(input.at(i) == 'u' || input.at(i) == 'U'){
        conversion.at(i) = 'X';
    }
      if(input.at(i) == 'v' || input.at(i) == 'V'){
        conversion.at(i) = 'Y';
    }
      if(input.at(i) == 'w' || input.at(i) == 'W'){
        conversion.at(i) = 'Z';
    }
      if(input.at(i) == 'x' || input.at(i) == 'X'){
        conversion.at(i) = 'A';
    }
      if(input.at(i) == 'y' || input.at(i) == 'Y'){
        conversion.at(i) = 'B';
    }
      if(input.at(i) == 'z' || input.at(i) == 'Z'){
        conversion.at(i) = 'C';
    }
    if(input.at(i) == ' '){
        conversion.at(i) = ' ';
    }

}
cout << conversion << endl;
return 0;

} }

I don't understand how to fix the error, and I'd like help on fixing it. 我不知道如何解决该错误,并且我需要帮助来解决它。

You're trying to set characters in the string conversion , which has zero length. 您正在尝试设置长度为零的字符串conversion中的字符。 That's why you get the error. 这就是为什么您得到错误。 All at() calls on that string are invalid. 该字符串上的所有at()调用均无效。

You need to first copy the input string into conversion , and only then modify that. 您需要先将输入字符串复制到conversion ,然后再进行修改。 Or reserve enough space and push_back letters into it. reserve足够的空间并将push_back字母放入其中。 Or simply use input all along. 或者只是一直使用input

You can also do the “shift by 3” in a less verbose way, and respecting the case. 您也可以以不太冗长的方式进行“ 3移”,并尊重大小写。

And you'd want to read an entire line of input at once, not just a single string – because such strings can never contain white space: that's the difference between getting a line vs just a string. 而且您想一次读取整行输入,而不是单个字符串-因为这样的字符串永远不能包含空格:这是获得一行与仅得到一个字符串之间的区别。

#include <iostream>
#include <string>

int main() {
  using namespace std;
  while (true) {
    string input;
    cout << "> " << flush;
    if (!getline(cin, input) || input.empty()) return 0;
    cin >> ws; // consume '\n'
    for (char &c : input) {
      char base = '\0';
      if (c >= 'a' && c<= 'z') base = 'a';
      else if (c >= 'A' && c <= 'Z') base = 'A';
      if (base) {
        c -= base;
        c = (c + 3) % ('z' - 'a');
        c += base;
      }
    }
    cout << "< " << input << endl;
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在抛出“std::out_of_range”的实例后调用终止? - terminate called after throwing an instance of 'std::out_of_range'? 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 抛出&#39;std :: out_of_range&#39;实例后调用终止 - terminate called after throwing an instance of 'std::out_of_range' 在为我的程序抛出一个 &#39;std::out_of_range&#39; 实例后调用 Terminate 来查找和替换字符串中的单词 - Terminate called after throwing an instance of 'std::out_of_range' for my program which finds and replaces words in a string 程序以信号 SIGABRT 终止,已中止。 在抛出 'std::out_of_range' 的实例后调用终止 - Program terminated with signal SIGABRT, Aborted. terminate called after throwing an instance of 'std::out_of_range' &quot;if (argc &lt; 2 || argc &gt; 2)&quot; 应该有 2 个参数吗? &amp; 在抛出“std::out_of_range”错误实例后调用终止 - Should "if (argc < 2 || argc > 2)" be fine with 2 arguments? & terminate called after throwing an instance of 'std::out_of_range' error 抛出“std::out_of_range”实例后调用终止(从未见过此错误) - terminate called after throwing an instance of 'std::out_of_range' (never seen this error) 在抛出 'std::out_of_range' 的实例后调用终止 what(): basic_string::at: __n 错误 - terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n error 在使用向量时抛出“std::out_of_range”错误实例后调用终止 - Terminate called after throwing an instance of 'std::out_of_range' error while using vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM