簡體   English   中英

為什么在這里出現out_of_range異常?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM