0

I'm writing a program to find Primary number. It works in Clion but it doesn't work in Visual Studio or other compiler... I really want to know it TT

#include <iostream>

using namespace std;

int main() {
    int num;
    int i;
    int j;
    int count = 0;
    int lcount = 0;

    do {
        cin >> num;
    } while (num < 1 || num > 100);

    int arr[num];
    for(i = 0; i<num; i++) {
        cin >> arr[i];
    }

    for (i = 0; i < num; i++) {
        if (arr[i] == 1)
            continue;

        for (j = 2; j < arr[i]; j++) {
            if (arr[i] % j == 0) {
                lcount = 1;
                break;
            }
        }
        if (lcount==0)
            count++;
    }

    cout << count;
    return 0;
}
Maxime
  • 838
  • 6
  • 18

1 Answers1

2

When you use

int arr[num];

you are using a variable length array since the value of num is not known at compile time and its value can be anything at run time. Variable length arrays are not supported by the standard. Some compilers support it as an extension. It appears that Visual Studio does not support it.

Change the line to use a std::vector.

std::vector<int> arr(num);
R Sahu
  • 204,454
  • 14
  • 159
  • 270