-5

I wrote a program that gets user input and checks if the user entered a vowel character. (These letters are vowels in English: A, E, I, O, U, and sometimes Y, and also the lower-cases.)

My problem is that I can't check if the user entered for example, 'a' and 'A'.

I Get this error:

case label value has already appeared in this switch at line 13.

My Program:

#include <iostream>

int main()
{
    int i = 0;
    char myString[100];
    std::cout << "Enter a String "; std::cin >> myString[100];
    if (std::cin.fail()) {
        std::cout << "Invalid Input!";
    } else {
        switch (myString[100])
        {
            case 'a' || 'A':
                i++;
                break;
            case 'e' || 'E':
                i++;
                break;
            case 'i' || 'I':
                i++;
                break;
            case 'o' || 'O':
                i++;
                break;
            case 'u' || 'U':
                i++;
                break;
        }
    }

    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
nakE
  • 362
  • 1
  • 13
  • 2
    https://stackoverflow.com/questions/68578/multiple-cases-in-switch-statement – Mat Dec 17 '19 at 11:20
  • 4
    I recommend you go back to your text-book, tutorial or class-notes and check them again. That's now how you do multiple cases in C++. If you don't have a good text-book, [here's a list of good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). Please invest in some good books (or take a few classes) to learn properly instead of just guessing. – Some programmer dude Dec 17 '19 at 11:20
  • 2
    Also both `std::cin >> myString[100]` and `switch (myString[100])` are wrong. It also seems like you want to input and check a string, but you currently input and check a single character. All in all it seems you're just attempting to learn-by-guessing, which isn't a very good way to learn things. Again, get some good books to read, or take a few classes (preferably both). – Some programmer dude Dec 17 '19 at 11:22
  • Does this answer your question? [How to Determine if a Character Is a Vowel](https://stackoverflow.com/questions/27480478/how-to-determine-if-a-character-is-a-vowel) – Lukas-T Dec 17 '19 at 11:23
  • @Damien Get a good book to start with English. [Vowels Characters in English](https://simple.wikipedia.org/wiki/Vowel) – nakE Dec 17 '19 at 11:40
  • 1
    @Damien Yeah, you yawning youths yell yonder, yet you unyieldingly yeeted your canyon kayak yesterday. Yield your yucky yapping, you yokel! Yech! – user253751 Dec 17 '19 at 12:05

1 Answers1

4

You are misunderstanding what the value of the expression 'a' || 'A' is! This is a logical test on whether either 'a' or 'A' are TRUE (that is, non-zero). As it only involves constants, it will be evaluated at compile time to the value 1, - as will all the other case values. Hence the error.

What you need is, instead of:

case 'a' || 'A':
    i++;
    break;

Use:

case 'a':
case 'A':
    i++;
    break;

And likewise for the other letters. (You can put the two case checks on the same line, if you prefer.)

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83