2015-01-21 3 views
0

나는 아래의 코드를 가지고 있지만 어떤 이유로 (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; 
} 
+0

으로

바꾸기. getline() 후에 단어가 selectword에 저장되지만 '\ 0'으로 끝나지 않습니다. – pwwpche

+0

std :: string :: iterator를 사용하지 않는 이유는 무엇입니까? 그게 뭐가 잘못 됐니? – Blacktempel

답변

0

에 관한 말하고 수정 된 프로그램 :

#include <string> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 

int countChar(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; 
    string noSpaces= string(); 
    while (selectedWord[i] != '\0') { 
     if (selectedWord[i] != ' ') { 
      noSpaces+=selectedWord[i]; 
     } 
     i++; 
    } 
    cout << "The word without spaces is: " << noSpaces << endl; 

    return 0; 
} 

int countChar(string str) 
{ 
    int i = 0; 
    while (str[i] != '\0') { 
     i++; 
    } 
    return i; 
} 
  1. 문자열에 문자를 추가하려면, 사용 :

    noSpaces+=selectedWord[i];

+0

std :: string을 null로 끝낼 필요가 없습니다. –

+0

당신은 닌자를 제대로 은퇴했습니다. 정말 고맙습니다. –

+0

안녕하세요, 젠장, 너무 많이. 두 줄을 사용했습니다. noSpaces = noSpaces + str [i]; 문자열 noSpaces = 문자열(); 그리고 내 프로그램이 이제 작동합니다! – galeontiger

0

여기 몇 가지 실수를 했으므로 string.lenght()를 사용할 수 있습니다. 여기에 대답은

#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); 
    int x= countChar(selectedWord); 
    cout << "The number of characters in the word " << selectedWord << " is " << endl; 

    int i=0; 
    int j=0; 
    string noSpaces; 
    selectedWord.end(); 
    for(int i=selectedWord.length()-1; i>=0; i--){ 
     if(selectedWord[i]!=' ') { 
      char c=selectedWord[i]; 
      noSpaces+=c; 
     } 
     j++; 
    } 
    cout << "The reverse of your selected word is: " << noSpaces << endl; 

    return 0; 
} 

int countChar(string str) 
{ 

    int i=str.length(); 
    /* while(str[i]!='\0') { 
     i++; 
    }*/ 
    return i; 
} 
0

이 질문에 유래 stackoverflow.com 의해 질문 C++ 두 번째로 만들 필요가 없습니다 std::string. 첫 번째 것을 다시 사용하지 않는 것 같습니다.

#include <string> 
//#include <cstdlib> 
#include <iostream> 


int main(void) 
{ 
    std::cout << "Select a word: " << std::endl; 
    std::string selectedWord; 
    std::getline(std::cin,selectedWord); 
    std::cout << "The number of characters in the word " << selectedWord << " is " << selectedWord.size() << std::endl; 

    for (std::string::iterator itr = selectedWord.begin(); itr != selectedWord.end(); ++itr) 
    { 
     if (*itr == ' ') //0x20 
     { 
      itr = selectedWord.erase(itr); 
     } 
    } 

    std::cout << "The reverse of your selected word is: " << selectedWord << std::endl; 

    return EXIT_SUCCESS; 
} 

+

noSpaces += selectedWord[i]; 
//or 
noSpaces.push_back(selectedWord[i]); 

귀하의 std::string noSpaces;이 장소에서 비어 있습니다. nospaces의 길이가 제로이기 때문에

1

이 줄,

noSpaces[j]=selectedWord[i]; 

정의되지 않은 동작을 일으 : 존재하지 않는 저장 장치를 색인하고 있습니다. 프로그램은 기능 countChar 추락

noSpaces.push_back(selectedWord[i]); 
관련 문제