![](/img/trans.png)
[英]Error Unhandled exception at 0x76C9FD62 in test.exe: Microsoft C++ exception: std::out_of_range at memory location 0x006FF870. occurred
[英]Unhandled exception at 0x77031D4D in Mini-Project.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00CFF620
我試圖為我的項目做一個密碼,我發現了getline的錯誤,因為我希望用'。'來填充字符串。 但是當我只輸入“。”時 錯誤顯示為“ Mini-Project.exe中0x77031D4D的未處理異常:Microsoft C ++異常:內存位置0x00C6F478的std :: out_of_range。” 我已經嘗試修復此錯誤了好幾個星期,但我做不到。 請告訴我我的錯誤。 我很高興學習編碼的新方法。 謝謝。
#include <iostream>
#include <string>
using namespace std;
class Encryption
{
private:
string encrypt;
string decrypt;
public:
Encryption();
Encryption(string,string);
void set_encryption(string&);
string encryption();
void set_decryption(string);
string decryption();
};
Encryption::Encryption()
{
encrypt = "";
decrypt = "";
}
Encryption::Encryption(string Encrypt,string Decrypt)
{
encrypt = Encrypt;
decrypt = Decrypt;
}
void Encryption::set_encryption(string &Encrypt)
{
encrypt = Encrypt;
}
string Encryption::encryption()
{
char a;
string text1(encrypt);
for (int i = 0; i <= encrypt.size() - 1; i++)
{
a = encrypt.at(i);
int b = (int)a;
b += 1;
//if (b > 254) { b = 254; }
a = (char)b;
text1.insert(i, 1, a);
}
string Encrypted(text1, 0, encrypt.size());
encrypt = Encrypted;
return encrypt;
}
void Encryption::set_decryption(string Decrypt)
{
decrypt = Decrypt;
}
string Encryption::decryption()
{
char c;
string text2(decrypt);
for (int i = 0; i <= (decrypt.size() - 1); i++)
{
c = decrypt.at(i);
int d = (int)c;
d -= 1;
c = (char)d;
text2.insert(i, 1, c);
}
string Decrypted(text2, 0, decrypt.size());
decrypt = Decrypted;
return decrypt;
}
int main()
{
while (1)
{
//initialize and get the string from the user
Encryption text;
char a,ans,text1c;
string text1, text2;
do
{
cout << "****Welcome to Subition Cpiher****" << endl;
cout << "This Program Only Allow Alphabet." << endl;
cout << "Please Enter \n1-Encrypt \n2-Decrypt\n3-Clear Screen \n0-End " << "\n:";
cin >> a;
}
while (a != '1' && a != '2' && a != '0' && a!='3');
if (a == '1')
{
again:char c;
cout << "Enter a string to encrypt <End with '.'>: ";
getline(cin, text1, '.');
c = text1.at(1);
if (c == '.')
{
cout << "You have not enter any string. Please try again." << endl;
goto again;
}
else
{
text1.erase(text1.begin());
text.set_encryption(text1);
cout << "The encrypted text is: " << text.encryption() << endl;
}
}
else if (a == '2')
{
cout << "Enter a string to decrypt <End with '.'>: ";
getline(cin, text2, '.');
text2.erase(text2.begin());
text.set_decryption(text2);
cout << "The decryted text is: " << text.decryption() << endl;
}
else if (a == '0')
{
cout << "Thank you for using the software." << endl;
system("pause");
return 0;
}
else if (a == '3')
{
system("cls");
}
}
}
更換
c = text1.at(1); 如果(c =='。')
與
如果(text1.size()== 1)
當你把。 您有一個由1個元素組成的帶有空字符串的數組,並嘗試執行text1.at(1),這將讀取不存在的內存塊,因此請不要這樣做。
玩得開心
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.