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!!