덤프 내 코드입니다 :분할 오류 핵심은 여기에
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string checkpalindrome(string sentence){
sentence.erase(std::remove(sentence.begin(), sentence.end(), ' '), sentence.end());
int unsigned i = 0;
for (i = 0; i < sentence.length(); i++){
sentence[i] = tolower(sentence[i]);
}
if (sentence == string(sentence.rbegin(), sentence.rend())){
cout << sentence << " is a palindrome." << endl;
}
if(sentence != string(sentence.rbegin(), sentence.rend())){
cout << sentence << " isn't a palindrome." << endl;
}
return 0;
}
int main(){
int x = 1;
string originalsentence;
while (x == 1){
int input;
string sentence;
cout << "Press 1 to reverse a string, 2 to check a palindrome, and anything else to quit: ";
cin >> input;
if(input == 1){
cout << "Enter a sentence to reverse: ";
cin.ignore(256, '\n');
getline(cin,originalsentence);
sentence = originalsentence;
cout << string(sentence.rbegin(), sentence.rend()) << endl;
}
if(input == 2) {
cout << "Enter a sentence to check: ";
cin.ignore(256, '\n');
getline(cin,originalsentence);
sentence = originalsentence;
checkpalindrome(sentence);
}
cout << "Hit 1 if you want to continue, anything else to quit: ";
cin >> x;
}
}
이 프로그램은 완료하고 나는 그것을 항상 맨 끝에, 옵션 2는 회문를 확인하기 위해 수행 할 때를 제외하고이, 어떻게해야 모든 일들을한다 코어 덤프. 나는 cin.ignore를 포함하여 시도했지만, 도움을받지 못했습니다. 어떤 아이디어?
편집 1 : 그래서 g ++이 던진 세 가지 경고를 변경했습니다. 그들은 palindrome.cpp:19: warning: comparison between signed and unsigned integer expressions palindrome.cpp:29: warning: no return statement in function returning non-void palindrome.cpp:35: warning: unused variable âaâ
int i를 부호없는 것으로 변경하고 28에서 0을 반환하고 삭제했습니다. 이제 컴파일 할 때 오류는 발생하지 않지만 실행하면이 오류와 함께 palindrome 검사가 완료 될 때 충돌이 발생합니다. terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct NULL not valid Abort (core dumped)
더 이상의 아이디어가 있습니까? 질문에서 복사
경고가 켜져있는 상태에서 컴파일을 시도 했습니까? 대부분의 컴파일러가 알아 차릴 것이라고 생각되는 문제가 있습니다. –
세그먼트 화 오버플로가 정확히 나타나는 위치를 확인하려면 디버그하십시오. – user1929959
그래서 오류를 수정하고 위의 코드를 업데이트하여 새로운 오류를 표시했습니다. – stormcynk