How we can write this function?

closed account (G187ko23)
write a function that accepts a train code of type character (lower or upper case)and returns the correct sponding train type to the following table.

Train code Train type
S or s Steam
D or d Diesel
E or e Electric
others Invalid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cctype>
std::string TrainType(char t)
{
  t = toupper(t);
  switch(t)
  {
  case 'S':
    return "Steam";
  case 'D':
    return "Diesel";
  case 'E':
    return "Electric";
  default:
    return "Invalid";
  }
}
Last edited on
Topic archived. No new replies allowed.