[英]Why do I get an out_of_range exception here?
當前正在uni進行一個項目,起初我需要對字符串進行WeirdPuncProgram.exe: Microsoft C++ exception: std::out_of_range at memory location 0x004EF898
連字符,這似乎很簡單,但是當我運行該程序時,它出現了WeirdPuncProgram.exe: Microsoft C++ exception: std::out_of_range at memory location 0x004EF898
錯誤WeirdPuncProgram.exe: Microsoft C++ exception: std::out_of_range at memory location 0x004EF898
它也沒有正確返回字符串值,在函數answer()
內更改了連字符,並刪除了連字符,但是一旦出現連字符,它就只能返回原始輸入。
#include <iostream>
#include <string>
using namespace std;
string answer;
string hyphonRemover(string answer)
{
string spacer = " ";
int h;
for (int i = 0; i < answer.length(); i++)
{
h = answer.find_first_of("-");
answer.replace(h, 1, spacer);
}
return answer;
}
int main()
{
cout << "Type a sentence which contains punctuation and ill remove it for you! " << endl << endl;
getline(cin, answer);
hyphonRemover(answer);
cout << answer << endl;
system("pause");
return 0;
}
hyphonRemover()
中answer
每次使用都是局部變量,而不是您上面定義的全局answer
。
因此該函數將僅修改其局部變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.