Problems with a program, switch statement

Hi i made a program with switch, if i input a character to show me if is vowel or a consonant if is letter and to show me if is odd or even if it's number
i don't understand where is the mistake..i need obligatory to use switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
  char n;
  cout<< "Input the character : "; cin>>n;
  switch(n){
  case 'A':
  case 'a':
  case 'E':
  case 'e':
  case 'I':
  case 'i':
  case 'O':
  case 'o':
  case 'U':
  case 'u':
  cout<<n<< " vowel ."<<endl; break;
  case 'B':case 'b':case 'C':case 'c':case 'D':case 'd':
  case 'F':case 'f':case 'G':case 'g':case 'H':case 'h':
  case 'J':case 'j':case 'K':case 'k':case 'L':case 'l':
  case 'M':case 'm':case 'N':case 'n':case 'P':case 'p':
  case 'Q':case 'q':case 'R':case 'r':case 'S':case 's':
  case 'T':case 't':case 'V':case 'v':case 'W':case 'w':
  case 'X':case 'x':case 'Y':case 'y':case 'Z':case 'z':
  cout<<n<< " is consonant ."<<endl; break;
  }
  switch(n%2==0){
  case 0: cout<<n<< " is even"; break;
  case 1: cout<<n<< " is odd"; break;
  default:cout<<n<< " is simbol"<<endl; break;
  
  }
  return 0;
}
Last edited on
https://www.cplusplus.com/articles/jEywvCM9/
Carry on with your cases
1
2
case '0':case'2'....
cout<<n<< " is even"; break;
@denispopescuo,
Are you reporting people who are actually assisting you in answering your questions?

As @salem c links:
- use code tags, so that we can run your code online.

As @salem c wrote:
- to get the different types of number then continue with the cases.

Stop hitting the Report button (as you've done in other posts) and start reading what people have written.
sorry, i'm new here
now i used code tags
Last edited on
Using required switch statements, with a few checks if the input is a letter or a numeric digit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cctype>   // ::isalpha, ::tolower, ::isdigit
// http://www.cplusplus.com/reference/cctype/

int main()
{
   std::cout << "Input a single character: ";
   char n;
   std::cin >> n;
   std::cout << '\n';

   // let's check if the input is a letter or a numeric digit

   if (::isalpha(n))  // is it a letter?
   {
      switch (::tolower(n))  // temp convert to lower case
      {
      case 'a': case 'e': case 'i': case 'o':  case 'u': // is it a vowel?
         std::cout << n << " is a vowel.\n"; break;

      default:  // if not, consonant
         std::cout << n << " is a consonant.\n"; break;
      }
   }
   else if (::isdigit(n))  // is it a numeric digit?
   {
      switch ((int)(n - '0') % 2)  // temp convert to integer
      {
      case 0: std::cout << n << " is a number and is even.\n"; break;

      case 1: std::cout << n << " is a number and is odd.\n"; break;
      }
   }
   else // not a letter or numeric digit
   {
      std::cout << n << " is symbol\n";
   }
}

When the input is a character converting to lower (or upper) case reduces the number of case statements required.

Also, when working with a letter if it isn't a vowel by default it is a consonant.

The is-a-number switch is not really needed since all you are looking for is whether a number is odd or even. It could be rewritten:
27
28
29
30
31
32
33
34
      if ((int) (n - '0') % 2)  // temp convert to integer
      {
         std::cout << n << " is a number and is odd.\n";
      }
      else
      {
         std::cout << n << " is a number and is even.\n";
      }
Last edited on
thank you
Topic archived. No new replies allowed.