0

I need help fixing this error. It is saying that X needs to be a constant. I have tried re-writing int x=0 as const int x=0, but it shows another error saying that x can't be constant. Does anyone know how to fix this error? So far, it is the only one in my code.

int main()
{
    int x = 0;
    ifstream inputFile; 
    inputFile.open("systemsprices.txt");
    string temp;
    while (!inputFile.eof()) { 
        getline(inputFile, temp); ++x;
    }
    x /= 2;

    string console[x]; //this x has an error
    float price[x];    // this x has an error

    loadArrays(console, price, x);


    while (1) 
    {
        int choice = showMenu();
        switch (choice)
        {
        case 1: showArrays(console, price, x); break;
        case 2: lookUpPrice(console, price, x); break;
        case 3: sortPrices(console, price, x); break;
        case 4: highestPrice(console, price, x); break;
        case 5: exit(0); break;
        }
    }
}
  • 1
    You cannot have an array with a runtime-dependent size in standard C++. Use `std::vector` instead of arrays. – user17732522 Jan 23 '22 at 18:56
  • You probably want a `std::vector console;` and a `std::vector price;` – πάντα ῥεῖ Jan 23 '22 at 18:57
  • 1
    Unrelated, but also a bug: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125) – Lukas-T Jan 23 '22 at 18:58
  • Unfortunately, this is an assignment that requires I write the program in parallel arrays. – Landan Allen Jan 23 '22 at 19:11
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default-initializing them and immediately overwriting the default values. In this case, that means changing `ifstream inputFile; inputFile.open("systemsprices.txt");` to `ifstream inputFile("systemsprices.txt");`. – Pete Becker Jan 23 '22 at 19:27
  • @LandanAllen If you are not allowed to use `std::vector`, then you need to dynamically allocate memory with `new[]`/`delete[]` or use a fixed-size array with a size that is always large enough for the largest input file that is allowed. – user17732522 Jan 23 '22 at 19:34

0 Answers0