0

Maybe a similar topic has already been discussed. But, I have a different problem. Here, I'm using C++ 20 in Visual Studio

I have code

#include<iostream>
using namespace std;

int main() {
    int a;
    cin >> a;
    switch (a) {
    case 1 ... 9:
        cout << "satuan" << endl;
        break;
    case 10 ... 99:
        cout << "puluhan" << endl;
        break;
    case 100 ... 999:
        cout << "ratusan" << endl;
        break;
    case 1000 ... 9999:
        cout << "ribuan" << endl;
        break;
    case 10000 ... 99999:
        cout << "puluhribuan" << endl;
        break;
    }
}

And the problem is

Build started...
1>------ Build started: Project: If Then or Case, Configuration: Debug x64 ------
1>If Then or Case.cpp
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(8,9): error C2143: syntax error: missing ':' before '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(8,9): error C2059: syntax error: '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(11,10): error C2143: syntax error: missing ':' before '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(11,10): error C2059: syntax error: '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(14,11): error C2143: syntax error: missing ':' before '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(14,11): error C2059: syntax error: '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(17,12): error C2143: syntax error: missing ':' before '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(17,12): error C2059: syntax error: '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(20,13): error C2143: syntax error: missing ':' before '...'
1>D:\Multimedia\Teknik Informatika - Komputer - Elektronika\C++\Visual Studio\If Then or Case\If Then or Case.cpp(20,13): error C2059: syntax error: '...'
1>Done building project "If Then or Case.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have tried my code in C++11 (Using DevC++) and it's work. But, when I tried in C++20 (Using Visual Studio) this error. How can I fix this problem?

  • [What is "..." in switch-case in C code](https://stackoverflow.com/q/18853502/995714), [In C++ can there be a range of values in a switch statement?](https://stackoverflow.com/q/25799925/995714), [How to let VS Code IntelliSense accept C++ switch-case range as a valid syntax?](https://stackoverflow.com/q/70314604/995714), [Switch statements with case ranges, is it possible?](https://stackoverflow.com/q/22522496/995714) – phuclv Feb 05 '22 at 04:10
  • `I have tried my code in C++11. And it's work. But, when I tried in C++20 this error.` it never works in MSVC in C++11 – phuclv Feb 05 '22 at 04:16
  • Now I know where I went wrong... Sorry about that... Before (when I'm tested C++11) I used DEVC++ and it use TDM GCC Compiler... Thanks for your correction. – UwUisMyLife Feb 05 '22 at 04:42

2 Answers2

2

This has nothing to do with C++20.

Case ranges with ... are not part of standard C++. It is a compiler-specific extension to the language from GCC.

MSVC does not support this extension, as far as I know, with any version.

user17732522
  • 53,019
  • 2
  • 56
  • 105
0

Hey you can use if else statement instead

#include<iostream>
using namespace std;

int main() {
    int a;
    cin >> a;

    if(a <= 9)
        cout << "satuan" << endl;
    else if(a <= 99)
        cout << "puluhan" << endl;
    else if(a <= 999)
        cout << "ratusan" << endl;
    else if(a <= 9999)
        cout << "ribuan" << endl;
    else if(a <= 99999)
        cout << "puluhribuan" << endl;
}