c ++ cout字符串不起作用。请指教

时间:2015-01-21 05:26:48

标签: c++ string cout

我有以下代码,但由于某种原因,最后一个实例(noSpaces字符串应该打印出来)不会因任何原因输出。

我已经确认整个程序(不包括那个输出字符串)工作正常。

注意:我说的是关于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;
}

4 个答案:

答案 0 :(得分:1)

这一行,

noSpaces[j]=selectedWord[i];

具有未定义的行为,因为nospaces的长度为零:您正在为不存在的存储编制索引。

替换为

noSpaces.push_back( selectedWord[i] );

答案 1 :(得分: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];

答案 2 :(得分:0)

你在这里犯了一些错误,你可以使用string.length()。这是答案

#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;
}

答案 3 :(得分:0)

这个问题标记为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;为空。