0

I am creating a NodeList Class but I have an error which is, E0028: Expression must have a constant value in my NodTest.cpp for these two lines:

string idx[n];
int valx[n];

And here is my code for NodTest.cpp linked to NodList.h:

#include "NodeList.h"
#include <iostream>
using namespace std;
int main() {
    int n, val;
    string id;
    cout << "Enter the num of items in list:";
    cin >> n;
    string idx[n];
    int valx[n];
    for (int i = 0; i < n; ++i) {
        cout << "Enter the identity and value of items:" << i + 1 << "\n";
        cin >> idx[i] >> valx[i];
    }
    NodeList myList(idx, valx, n);

    int ch;
    while (1) {
        cout << "\nMENU:\n1.Add item in the list\n2.check for an item\n3.print the list\n4.exit\n";
        cin >> ch;
        if (ch == 1) {
            cout << "Enter identity and value of item:\n"; cin >> id >> val;
            if (myList.insert(id, val)) {
                cout << "Inserted Successfully\n";
            }
            else {
                cout << "Cant Insert, List overflow\n";
            }
        }
        else if (ch == 2) {
            cout << "Enter 1 for searching by number\nEnter 2 for searching  by {identity,val}\n";
            cin >> ch;
            if (ch == 1) {
                cout << "Enter the value:\n";
                cin >> val;
                if (myList.check(val)) {
                    cout << "Items found:\n";
                }
                else cout << "Item does not found\n";
            }
            else if (ch == 2) {
                cout << "Enter the identity and value:\n";
                cin >> id >> val;
                if (myList.check(make_pair(id, val))) {
                    cout << "Items found:\n";
                }
                else cout << "Item does not found\n";
            }
            else break;
        }
        else if (ch == 3) {
            cout << "\nThe List is\n";
            myList.print();
        }
        else break;
    }
    return 0;
}
#include "NodeList.h"
#include <iostream>
using namespace std;
int main() {
    int n, val;
    string id;
    cout << "Enter the num of items in list:";
    cin >> n;
    string idx[n];
    int valx[n];
    for (int i = 0; i < n; ++i) {
        cout << "Enter the identity and value of items:" << i + 1 << "\n";
        cin >> idx[i] >> valx[i];
    }
    NodeList myList(idx, valx, n);

    int ch;
    while (1) {
        cout << "\nMENU:\n1.Add item in the list\n2.check for an item\n3.print the list\n4.exit\n";
        cin >> ch;
        if (ch == 1) {
            cout << "Enter identity and value of item:\n"; cin >> id >> val;
            if (myList.insert(id, val)) {
                cout << "Inserted Successfully\n";
            }
            else {
                cout << "Cant Insert, List overflow\n";
            }
        }
        else if (ch == 2) {
            cout << "Enter 1 for searching by number\nEnter 2 for searching  by {identity,val}\n";
            cin >> ch;
            if (ch == 1) {
                cout << "Enter the value:\n";
                cin >> val;
                if (myList.check(val)) {
                    cout << "Items found:\n";
                }
                else cout << "Item does not found\n";
            }
            else if (ch == 2) {
                cout << "Enter the identity and value:\n";
                cin >> id >> val;
                if (myList.check(make_pair(id, val))) {
                    cout << "Items found:\n";
                }
                else cout << "Item does not found\n";
            }
            else break;
        }
        else if (ch == 3) {
            cout << "\nThe List is\n";
            myList.print();
        }
        else break;
    }
    return 0;
}

Please help me figureout where is the problem with (n)!! I am using VS not any other programming environment!!

  • 2
    array sizes must be compile time constants. Use `std::vector` if you know the size only at runtime – 463035818_is_not_an_ai Jan 19 '21 at 15:52
  • some compilers have Variable Length Arrays as extension, this is used frequently in certain tutorials and often confused to be proper C++, but it isnt. In case thats why you expected your code to be fine, here you can find more details: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Jan 19 '21 at 15:54

0 Answers0