A problem

I tried to make a console app that reads every character and it gives u it type
I'm using visual studio 2022
the IDE indicates an error in "..."

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
 #include <iostream>
#include <string>
#include <windows.h>
#include <mmsystem.h>
#include <math.h>

using namespace std;


int main()

{
    char c;
    cout << "enter a character  : ";
    cin >> c;
    switch (c)
    {
         
    case 'A'...'Z':cout << "uppercase"; break;
    case 'a'...'z':cout << "min"; break;
    case '0'...'9':cout << "num"; break;
    default: cout << "error";


    }

       

    system("pause");
        return 0;


    

}
Who told you that "case 'A'...'Z':" is valid case syntax?
Last edited on
what is the valid one then
I wouldn't use switch/case for this, it's too restrictive.

Try something like this instead
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
#include <iostream>
using namespace std;

int main()
{
    char ch;
    cout << "enter a character  : ";
    cin >> ch;
    
    if ('a' <= ch && ch <= 'z')
    {
        cout << "lowercase\n";
    }
    else if ('A' <= ch && ch <= 'Z')
    {
        cout << "uppercase\n";
    }
    else if ('0' <= ch && ch <= '9')
    {
        cout << "num\n";   
    }
    else
    {
        cout << "error\n";   
    }
}


inb4 something about ASCII vs EBCDIC on some esoteric computer
Last edited on
thank you so much it works but I tried my code in another IDE called dev c++ and it worked perfectly.
Okay so, there is a C extension in GCC that allows for ellipses in case statements. Apparently this bleeds over into GCC's C++ implementation as well. Dev-C++ is most likely using a Windows port of GCC called MinGW.

In other words, it's a non-standard extension, which is why it works on some compilers but not others.
Yeah I got you.
thanks for helping
No problem, BTW, the following is an alternative solution that uses <cctype> header functionality.
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
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    char ch;
    cout << "enter a character  : ";
    cin >> ch;
    
    if (isupper(ch))
    {
        cout << "uppercase\n";
    }
    else if (islower(ch))
    {
        cout << "lowercase\n";
    }
    else if (isdigit(ch))
    {
        cout << "num\n";   
    }
    else
    {
        cout << "error\n";   
    }
}
Functions from <cctype>
To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char https://en.cppreference.com/w/cpp/string/byte/isupper#Notes
Topic archived. No new replies allowed.