哨兵不工作?

时间:2014-03-09 19:56:02

标签: c++11

我不确定为什么这不起作用,但这是我的错误信息:

错误]'operator!='不匹配(操作数类型为'std :: string {aka std :: basic_string}'和'const int')

编辑:上述问题已得到解决。但是,目前的问题是多余的***,并且句子中没有元音删除而不仅仅是句子的第一个单词。

#include <iostream>
#include <string>
using namespace std;


void removeVowel(string&);      // Removes vowels from input string.
string withVowel;               // Will be used to read user input. 

int main ()
{   

    const string SENTINEL = "0";        // Sentinel value. 

    // Request input string unless SENTINEL is entered.  

    cout << "Enter a word or series of words. " << '\n';
    cout << "Or, enter " << SENTINEL << " to quit. " << endl;
    cin >> withVowel;

    // In case of SENTINEL:

    while (withVowel == SENTINEL)
    {
        cout << "***" << endl;
    }

    // Run loop.

    removeVowel(withVowel);

    // Display the string without vowels.

    cout << "The word(s) entered reflecting only consonants: " << withVowel << endl;

    return 0;
}

    void removeVowel(string& withVowel)
    {
        int i = 0;
        int length = int(withVowel.length());
        while (i < length)
    {
        if (withVowel.at(i) == 'a' || 
            withVowel.at(i) == 'A' || 
            withVowel.at(i) == 'e' || 
            withVowel.at(i) == 'E' ||
            withVowel.at(i) == 'i' || 
            withVowel.at(i) == 'I' || 
            withVowel.at(i) == 'o' || 
            withVowel.at(i) == 'O' || 
            withVowel.at(i) == 'u' ||
            withVowel.at(i) == 'U')

            {
                withVowel.erase(i, 1);
                length = int(withVowel.length());
            }
            else i++; 
        }

    // Display the string without vowels.   

    cout << removeVowel << endl;

    } 

2 个答案:

答案 0 :(得分:1)

根据your other question和错误消息,我认为withVowelstd::string。错误消息几乎告诉您问题所在:您无法将std::stringint进行比较。

由于您只需SENTINEL进行打印和比较,因此只需将其声明为std::string

const std::string SENTINEL = "0";

答案 1 :(得分:0)

您无法将 const int 字符串进行比较。 使用 ctrl + z 输入来停止输入。

string word;    
cout << "ctrl+z and Enter to exit\n";
while (cin >> word){
    cout << word << ' ';
    // other processing
}

对于你的情况:

cout << "Enter a word or series of words.\n";     
cout << "Ctrl+z and Enter to exit\n";
while (cin >> withVowel){
    removeVowel(withVowel);
    cout << "The word(s) entered reflecting only consonants :" << withVowel << endl;
}