【发布时间】:2020-06-23 14:45:44
【问题描述】:
此代码是关于将字符串转换为替代密码(将特定字母替换为另一个特定字母,例如:'E' for 'a')当我输入字符串时,它可以正常工作。但是当我输入一个句子时,它说'Mterminating call after throwing a moment of 'std::out_of_range''。什么是hapeening?我该怎么办?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"EFGHIJKLMNOPQRSTUVWXYZ1234efghijklmnopqrstuvwxyz5678"};
string secret_massage {};
string encrypted_massage {} ;
cout << "Enter your secret massege : ";
getline(cin, secret_massage) ;
cout << "Encrypting massage . . . " << endl;
cout << "Encrypted massege : " ;
for ( size_t i {0} ; i < secret_massage.length() ; ++i ){
char selection {};
int j {};
selection = secret_massage[i];
j = alphabet.find(selection);
encrypted_massage = key.at(j);
cout << encrypted_massage;
} [enter image description here][1]
cout << endl;
return 0;
}
【问题讨论】:
标签: c++ string loops output c++14