나는 아래의 코드를 가지고 있지만 어떤 이유로 (noSpaces 문자열이 출력되어야하는) 마지막 인스턴스가 어떤 이유로 든 출력되지 않습니다.C++ cout 문자열이 작동하지 않습니다. 조언을주십시오
나는 이미 하나의 출력 문자열을 제외한 전체 프로그램이 완벽하게 작동하고 있음을 확인했습니다.
참고 : 나는 라인 바로 여기에 대한 return 0
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;
int countChar(string str);
void removeSpaces(string str);
int main()
{
cout << "Select a word: ";
string selectedWord;
getline(cin,selectedWord);
cout << "The number of characters in the word " << selectedWord << " is " << countChar(selectedWord) << endl;
int i=0;
int j=0;
string noSpaces;
while(selectedWord[i]!='\0') {
if(selectedWord[i]!=' ') {
noSpaces[j]=selectedWord[i];
j++;
}
i++;
}
cout << "The reverse of your selected word is: " << noSpaces << endl;
return 0;
}
int countChar(string str)
{
int i=0;
while(str[i]!='\0') {
i++;
}
return i;
}
으로
바꾸기. getline() 후에 단어가 selectword에 저장되지만 '\ 0'으로 끝나지 않습니다. – pwwpchestd :: string :: iterator를 사용하지 않는 이유는 무엇입니까? 그게 뭐가 잘못 됐니? – Blacktempel