2015-01-13 1 views
-1

나는 사용자가 그 때 문장에서 어떤 정류장든지 제거하고 그 후에 그 문자열에있는 각 개별 단어의 목록을 만들 것입니다 문장을 입력 할 수있는 프로그램을 작성하고 있습니다 그 목록을 콘솔에 출력하기 전에. Project6.exe에서 0x7696B727에서에서프로그램 던지는 예외

처리되지 않은 예외 :

프로그램은 한가 단 1 마침표는 문장에 있지만 더 이상이있는 경우는이 예외를 throw보다으로 완벽하게 작동되는 마이크로 소프트 C++ 예외 : 메모리 위치 0x0022F8B8의 std :: out_of_range.

하고 내가 그것을 던져를 계속 실행하는 경우 :

실행 - 시간 검사 실패 # 0

- ESP의 값은 제대로 함수 호출을 통해 저장되지 않았습니다. 이것은 대개 다른 호출 규칙으로 선언 된 함수 포인터로 하나의 호출 규칙으로 선언 된 함수를 호출 한 결과입니다.

제안 사항? 누군가가 요청하기 전에 (그리고, 난 당신이 보통 문장 1 마침표를 알고하지만 난 테스트의 일환으로 1 개 이상으로 할 필요가 여기

는 코드입니다.

#include <iostream> 
#include <string> 

using namespace std 

string sSentence; // sets up a string variable called sSentence 

int i = 0; 

void RemoveFullStop() 
{ 
    while(sSentence.find (".") != string::npos) // the program runs this loop until it cannot   find any more full stops in the string 
{ 
     i = sSentence.find("." , i); // find the next full stop in the string and get the  character count of the space and place it in the variable i 
     sSentence.replace(i,1,""); // remove the full stop 
    } 
} 

void ListWords() 
{ 
    while(sSentence.find (" ") != string::npos) // the program runs this loop until it cannot find any more spaces in the string 
    { 
     i = sSentence.find(" " , i); // find the next space in the string and get the character count of the space and place it in the variable i 

     // cout << i << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used) 

     sSentence.replace(i,1,"\n"); 

     // cout << sSentence << endl; // output the contents of iWordSpace to the console (used for debugging - no longer used) 

     } 
    } 

    int main() 
    { 
     getline(cin, sSentence); // get user input and store it in sSentence (using the getline  function so the .find operation works correctly) 

     RemoveFullStop(); // calls the RemoveFullStop void function that removes all full stops from the string 

     ListWords(); // calls the ListWords void function that splits the string into a list of words 

     cout << endl; // formatting line 
     cout << "The words that were in the sentence were:" << endl; 
     cout << endl; // formatting line 
     cout << sSentence << endl; 
     cout << endl; // formatting line 

    system("pause"); 

    return 0; 
} 
+6

글쎄, 디버거의 코드를 단계별로 살펴 보셨습니까? – OldProgrammer

+3

@OldProgrammer 여기서 한 번의 클릭으로 표준 주석을 여러 번 써야합니다 : -P ... –

+1

while() 루프가 진행됨에 따라'sSentence'와'i'의 값을 출력 할 수 있습니다. 이것은 무슨 일이 일어나고 있는지에 대한 통찰력을 줄 것입니다. – jwd

답변

0

문제가. 당신이 RemoveFullStopListWords 모두에서 i을-사용하여 재를 유지하는 것이

i 만 이제까지 증가하고, 그래서 결국은 문자열의 끝을지나 얻을 수 있습니다.

당신은 정말 태어나 셨 안 d i이 작업을 수행하려면 변수가 필요합니다.

0

ListWords에서 sSentence.find(" " , i)이 실행될 때 값이 0이 아닌 것은 이미 RemoveFullStop()에 정의되어 있기 때문입니다. int i = 0;를 제거 첫째,이 문제를 해결하려면, 일반적으로 카운터를 의미하는이 변수 ii,j,k으로 내가 전화하지 것입니다, 다음 RemoveFullStop()에 추가하고 또한 ListWords()
, 이것은 단지 스타일 일 동안 실행하도록 코드 능력에 영향을주지 않습니다 . 이 변수를보다 적절하게 설명 적으로 호출하십시오.

다음은 관련 코드입니다.

using namespace std 
string sSentence; 

void RemoveFullStop() 
{ 
    int charPlace = 0; 
    while(sSentence.find (".") != string::npos) 
    { 
     charPlace = sSentence.find("." , charPlace); 
     sSentence.replace(charPlace,1,""); 
    } 
} 

void ListWords() 
{ 
    int charPlace = 0; 
    while(sSentence.find (" ") != string::npos) 
    { 
     charPlace = sSentence.find(" " , charPlace); 
     sSentence.replace(charPlace,1,"\n"); 
    } 
} 
+0

감사합니다. 완벽하게 작동했습니다. 나는 당신의 대답을보고 난 후에 그것을 보았습니다. 많은 감사 –

관련 문제