简体   繁体   中英

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:

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. That's why you get the error. All at() calls on that string are invalid.

You need to first copy the input string into conversion , and only then modify that. Or reserve enough space and push_back letters into it. Or simply use input all along.

You can also do the “shift by 3” in a less verbose way, and respecting the case.

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM