1

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=="

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • 1
    There is no `do {...} if()`. Take out the `do {}`. After the `if` (remove the `;`), add `{ return 0; }` to end the program here. And then you need to actually call the function on your sentence: `countVowels(sentence, 0)`. – BoBTFish Mar 01 '16 at 07:20
  • Oh, and you can't compare a `std::string` against a single character. Easiest fix is just to replace `'q'` with '"q"` (string literal), and same for `'Q'`. – BoBTFish Mar 01 '16 at 07:28
  • 1
    Also at the end of countVowels() you need to `return numVowels;` – Cârnăciov Mar 01 '16 at 07:29
  • And while we're at it, please reconsider your use of [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html). – BoBTFish Mar 01 '16 at 07:29
  • 1
    `<< countVowels` means to output `1`. Instead you need to be calling the function. (To call a function , place parentheses after the function name, with arguments inside the parentheses). – M.M Mar 01 '16 at 08:58
  • Does this even compile? You may want to check your reference text on how `do .. while` works! :) – CompuChip Mar 01 '16 at 09:01

1 Answers1

3

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.

BoBTFish
  • 19,167
  • 3
  • 49
  • 76