NaritaDogFight
NaritaDogFight

Reputation: 43

Code keeps printing "1" when everything is correct

The code runs and all but it doesn't print out the vowels, but instead prints a "1".

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

}

The output should be like this:

Enter a sentence or a to quit: I like apples!

There are 4 vowels in your sentence, and 11 letters.

Enter a sentence or q to quit: q

Bye!

My problem: Can someone explain to me why it keeps printing a "1", and my "if" statement where I am supposed to assign the hotkey "q" to exit the program isn't working. When I run the program I get an error at the if statement saying "no match for operators=="

Upvotes: 1

Views: 226

Answers (1)

BoBTFish
BoBTFish

Reputation: 19767

I usually don't like just providing a full solution, but since your question shows you have made a good effort, here's how I would write it (well, not quite, I simplified a little to be more beginner friendly):

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

Hopefully the comments make it all clear.

Now you might have been told that you can't use the standard algorithms (std::count_if), since part of the exercise seems to be to write that. Well I'll leave that to you. Your current version is close to correct, but remember to return the result. And you don't really need to pass in the numVowels count, just create that within the function, and remember to return it.

Upvotes: 3

Related Questions