characters in a string using "Case"

I'm having a difficult time with an exercise in my c++ book. I'm asked to identify how many times the letters 'a', 'e', 'i' appear in a input-ed string. And it gives me the constraint to use "Case". I have no idea how to start, there is only 1 example with "case" in the book and it's used with "switch" however the example is completely unrelated to my problem of characters in a string.

I would really appreciate if someone could give me some pointers as to how I should go about it to solve this problem, thanks.
By step:
a) Read string from input.
b) Pass through the string letter per letter (=char per char).
c) For each char, check if it's an 'a', 'e' or 'i'. Increase counter.
d) Output.

For step c, you could either use if's
if (charX == 'a') { ... }
or switch/cases
1
2
3
4
switch(charX) {
  case a: ... break;
  case i: ... break;
}

For only three different letters, the difference is minimal. For larger numbers of letters, a switch() becomes much more elegant (and efficient) compared to many ifs.
Thanks, I already knew how to do it with "if", but I don't see the logic behind "switch", perhaps because there are insufficient examples.

My main problem with switch in this exercise is the instruction you have to write before every break; in order to count the total number of times these characters appear in the string. Also how does the switch go through the wanted string? It's really confusing to me.

A switch is just an alternative way to write a set of if's. It doesn't do anything more (or anything less) than the if statements. It doesn't go through the string: you'll have to make your own loop for that. And for each char in the string, you'll need a switch.
Thanks for your help, it seems that I lack the c++ knowledge to do this one so I'm gonna skip to the next exercise.
Topic archived. No new replies allowed.