Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “代码持续打印”;1“;一切正常时_C++ - Fatal编程技术网

C++ “代码持续打印”;1“;一切正常时

C++ “代码持续打印”;1“;一切正常时,c++,C++,代码运行,但它不打印元音,而是打印一个“1” #包括 #包括 使用名称空间std; int count元音(字符串句子,int num元音) { 对于(int i=0;i ws; getline(cin,句子); } 如果(句子=='q'|句子=='q'); cout我通常不喜欢只提供一个完整的解决方案,但由于您的问题表明您已经做出了很大的努力,下面是我将如何编写它(嗯,不完全是这样,我简化了一点,以便对初学者更友好): #包括 #包括 #包括 布尔元音(字符c) { //如果传入的字符匹配,则

代码运行,但它不打印元音,而是打印一个“1”

#包括
#包括
使用名称空间std;
int count元音(字符串句子,int num元音)
{
对于(int i=0;i ws;
getline(cin,句子);
}
如果(句子=='q'|句子=='q');

cout我通常不喜欢只提供一个完整的解决方案,但由于您的问题表明您已经做出了很大的努力,下面是我将如何编写它(嗯,不完全是这样,我简化了一点,以便对初学者更友好):

#包括
#包括
#包括
布尔元音(字符c)
{
//如果传入的字符匹配,则返回true的简单函数
//元音列表中的任何一个,并在任何其他输入中返回false。
如果('a'==c||
‘e’==c||
‘i’==c||
'o'==c||
‘u’==c||
“A”==c||
‘E’==c||
‘I’==c||
'O'==c||
‘U’==c){
return true;//如果是元音,则返回true
}
return false;//如果不是,请记住返回false
}
std::size\u t count元音(std::字符串常量和句子)
{
//使用标准的count_if算法在字符串上循环并计数
//谓词为其返回true的所有字符。
//请注意,我们返回的结果是总计。
返回std::count_if(std::begin(句子),
结束(句子),
(元音);
}
int main(){
字符串句;

std::难道没有
do{…}if()
。取出
do{}
。在
if
(删除
)之后,添加
{return 0;}
以在此处结束程序。然后您需要实际调用语句上的函数:
countvowers(语句,0)
。哦,你不能将
标准::字符串
与单个字符进行比较。最简单的修复方法是将
'q'
替换为''q'''(字符串文字),对
'q'
也是如此。同样在countvonels()的末尾您需要
返回numowels;
,在我们进行此操作时,请重新考虑您对和的使用。
非常好的答案。正确、简单,对未来的读者非常有用。我喜欢这样!
#include <iostream>
#include <string>
using namespace std;

int countVowels(string sentence,int numVowels)
{
for(int i =0; i<sentence.length(); i++)
{
    if((sentence[i]==('a'))||(sentence[i]==('e'))||(sentence[i]==('i'))||(sentence[i]==('o'))||(sentence[i]==('u'))||(sentence[i]==('A'))||(sentence[i]==('E'))||(sentence[i]==('I'))||(sentence[i]==('O'))||(sentence[i]==('U')))
        numVowels=numVowels+1;

}

}

int main()
{
string sentence;
int numVowels = 0;
do{
cout << "Enter a sentence or q to quit: ";
cin >> ws;
getline(cin,sentence);
}
if(sentence == 'q'|| sentence == 'Q');



cout << "There are " << countVowels << " vowels in your sentence." << endl;

return 0;

}
#include <algorithm>
#include <iostream>
#include <string>

bool isVowel(char c)
{
    // A simple function that returns true if the character passed in matches
    // any of the list of vowels, and returns false on any other input.
    if ( 'a' == c ||
         'e' == c ||
         'i' == c ||
         'o' == c ||
         'u' == c ||
         'A' == c ||
         'E' == c ||
         'I' == c ||
         'O' == c ||
         'U' == c) {
        return true; // return true if it's a vowel
    }

    return false; // remember to return false if it isn't
}

std::size_t countVowels(std::string const& sentence)
{
    // Use the standard count_if algorithm to loop over the string and count
    // all characters that the predicate returns true for.
    // Note that we return the resulting total.
    return std::count_if(std::begin(sentence),
                         std::end  (sentence),
                         isVowel);
}

int main() {
    std::string sentence;
    std::cout << "Please enter a sentence, or q to quit: ";
    std::getline(std::cin, sentence);

    if ( "q" == sentence ||
         "Q" == sentence) {
        // Quit if the user entered a string containing just a single letter q.
        // Note we compare against a string literal, not a single character.
        return 0;
    }

    // Call the counting function and print the result.
    std::cout << "There are "
              << countVowels(sentence) // Call the function on the input.
              << " vowels in your sentence\n";
    return 0;
}