0

I get This error on VS 2022 but I ran the program on VS code and on online compilers just fine with no errors.

Error (active)  E0028   expression must have a constant value   line 64 
the value of variable "x" (declared at line 54) cannot be used as a constant

I don't really understand what the error means but the code just ran with no problems on other programs/compilers. I get the feeling that maybe that compiler is more strict so I made a mistake, but if that's the case why did it run normally on other compilers?

This is the code

#include <iostream>
#include<string>
using namespace std;


class persondata
{
private:
    string lastname, firstname, address, city, phone;
public:
    persondata(string In, string fn, string a, string c, string p) {
        setPData(In, fn, a, c, p);
    }

    void setPData(string In, string fn, string a, string c, string p) {
        firstname = In;
        lastname = fn;
        address = a;
        city = c;
        phone = p;
    }
    void printperson()
    {
        cout << firstname << endl << lastname << endl << address << endl << city << endl << phone;
    };
};
class CustomerData :public persondata
{
private:
    int customernum;
    bool mailinglist;
public:
    CustomerData(string In, string fn, string a, string c, string p) : persondata(In, fn, a, c, p)
    {
        customernum = 0;
        mailinglist = false;
    }
    CustomerData(string In, string fn, string a, string c, string p, int num, bool z) : persondata(In, fn, a, c, p)
    {
        customernum = num;
        mailinglist = z;
    }

    void printcust()
    {
        printperson();

        cout << customernum << endl << mailinglist;
    };
};


int main() {
    int x;
    string In;
    string fn;
    string a;
    string c;
    string p;
    int num;
    bool z;
    cout << "enter num: ";
    cin >> x;
    CustomerData* s[x];  // I get the error here
    for (int i = 0; i < x; i++)
    {
        cin >> In >> fn >> a >> c >> p >> num >> z;
        s[i] = new CustomerData(In, fn, a, c, p, num, 2);
    }
    for (int i = 0; i < x; i++)
    {
        s[i]->printcus();
    }

    return 0;
}



I'm not sure weather it's because the VS compiler is more strict or if it's a bug or a problem in the program.

0 Answers0