0

I am working through Ivor Horton's Beginning Visual C++ 2012, and I am doing exercise two in chapter three: Decisions and Loops. Here is my code:

#include <iostream>

using namespace std;

int main()
{
    char letter;
    int vowels = 0;

    do
    {
        cout << "Enter a letter, and enter q or Q to end" << endl;
        cin >> letter; // Enter a letter
        switch (letter)
        {
            case 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U': // If letter is a vowel, add to vowels variable
                vowels++;
                break;
            default: // If letter is not a vowel, break loop
                break;
        }
    } while (letter != 'Q' || letter != 'q');

    cout << "You entered " << vowels << " vowels.";
    return 0;
}

The intended purpose of this program is to allow the user to enter a letter until they enter q or Q, at which point the do...while loop ends and the program displays to the user the number of vowels they entered.

When I run this, the program does not quit when I enter Q or q. Why? How can I fix this?

qbush
  • 712
  • 2
  • 9
  • 28
  • `letter` is never equal to both `'Q'` and `'q'` – milleniumbug Jan 09 '16 at 17:06
  • 1
    Not what you asked (so posting as a comment), but `case 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U':` means the same thing as `case '\1':` and doesn't check for vowels. –  Jan 09 '16 at 17:10
  • "*Loop is Not Closing When Condition is Fulfilled*" is a bold claim; suggesting the compiler is not generating your code correctly. Occam's razor should always suggest your code is at fault. In fact the condition *always* true. A better title would be "Why does the loop never meet the terminating condition?" i.e. "*Why am I wrong?*" rather the "*Why is the compiler wrong?*" – Clifford Jan 09 '16 at 17:12
  • Possible duplicate of [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Davis Herring Jan 07 '18 at 06:22

4 Answers4

5

letter != 'Q' || letter != 'q'

means...

letter is not Q or is not q... so it's always true... as if it's Q, it will not be q, and if it's q, it will not be Q ... that would be only false, if letter would be Q and q at once.

Just use:

letter != 'Q' && letter != 'q'

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
2

De Morgan's laws.

!( letter == 'Q' || letter == 'q' ) <=> ( letter != 'Q' && letter != 'q' )

De Morgan's laws states,

Not (A or B) is equivalent to Not A and Not B.

Mars
  • 4,677
  • 8
  • 43
  • 65
1

letter != 'Q' || letter != 'q'

That's always true. You mean &&.

And NB for the case : it doesn't work as is, you could write case 'a': case 'A' ... : => all the vowel cases would fall to the vowels++;

Ilya
  • 5,377
  • 2
  • 18
  • 33
-1

Try this

#include <iostream>

using namespace std;

int main()
{
    char letter;
    int vowels = 0;
    int flag = 0;

    do
    {
        cout << "Enter a letter, and enter q or Q to end" << endl;
        cin >> letter; // Enter a letter
        switch (letter)
        {
            case 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U': // If letter is a vowel, add to vowels variable
                vowels++;
                break;
            case 'q' || 'Q' :  flag = 1;    
            break;
            default: // If letter is not a vowel, break loop
                break;
        }
    } while (flag !== 1);

    cout << "You entered " << vowels << " vowels.";
    return 0;
}