dimanche 24 octobre 2021

How do I make a clean if statement with multiple clauses?

I am tasked to create a little scrabble game and one of my "micro" tasks is computing the score of a given word based on a chart with letter associated with points:

Points Letters

1: a, e, i, l, n, o, r, s, t, u

2: d, g

3: b, c, m, p

4: f, h, v, w, y

5: k

8: j, x

10: q, z

Anyways, My first thought was just looping through the given word and checking each letter with an if loop.

for(auto ch: word){
  // as an example this if loop would check if the letter the for loop is on is an 'a' or 'e' or .... 
  // and then adds 1 point to integer 'score' as defined by the bale above
  if(ch == a || ch == e || ch == i || ch == l){
    score = score + 1;
  }
}

So my question is, is there a cleaner way of making that if loop? something like: if(ch == a,e,i,l...) something like that? For my task I am only allowed to use c++17 but I am fairly new to c++ and am not aware of its fullest potential. Hopefully one of you can tell me what is possible. Thank you

Aucun commentaire:

Enregistrer un commentaire