-3

I wrote the following code on visual studio and it says "expression did not evaluate to a constant" for [numWashes] on line 20, however when I try it on other compiler it works, how can I fix this?

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

struct WashData {
    int membershipStatus;
    int carSize;
    int washType;
    int additionalService;
    double totalCharge;
};


int main() {
    int getNumWashes;
    cout << "Enter the number of car washes: ";
    cin >> getNumWashes;
    const int numWashes = getNumWashes;
    // Create an array of WashData structures
    WashData washes[numWashes]; //line 20 **error**


    return 0;
}
  • 1
    use `std::vector`: `std::vector washes(numWashes);` from the `` header. – chrysante Mar 18 '23 at 07:41
  • 1
    *on other compiler it works*. That other compiler is accepting code that is not legal C++, this compiler is doing the right thing. In C++ arrays must have constant size. If you want an array like object with variable size use a `std::vector`. – john Mar 18 '23 at 08:17
  • 1
    `WashData washes[numWashes]`, where `numWashes` is a variable is not part of standard C++ at all. Some C++ compilers support such variable-length arrays (VLAs) as a *non-standard* extension. You are better off using `std::vector washes(numWashes)` which (unlike a VLA) gives you a *resizeable* collection, with initial size `numWashes`. – Peter Mar 18 '23 at 09:16
  • I tested your code compiler will prompt E0028, I suggest you refer to the solution in this [issue](https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value). – Yujian Yao - MSFT Mar 20 '23 at 02:36

1 Answers1

1

If you want to create a collection of a type of object with a non-compile-time-known size and default-initialize all the entries, then include the vector standard header and do std::vector<WashDate> washes(numWashes);. Also, I don't see a point in the intermediate numWashes variable that just takes the same value as getNumWashes.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Arguably having the intermediate variable allows for `numWashes` to be const. – chrysante Mar 18 '23 at 08:05
  • It works! Thank you so much for the help. I added getNumWashes because I was trying everything to fix the code, I will delete this variable now. – user21424638 Mar 18 '23 at 08:06
  • @chrysante yes, but I don't see any added value from that. – starball Mar 18 '23 at 08:26
  • @user - Apparently it is an attempt to get a compile time constant, as required for the array declaration. Of course it only creates a runtime constant, so a failed attempt. – BoP Mar 18 '23 at 11:25
  • 1
    @chrysante -- yes, `numWashes` is `const`, but the size of an array has to be a **compile-time** constant. "You're not allowed to modify this value" is not the same as "this value is known at compile time". – Pete Becker Mar 18 '23 at 13:09
  • @PeteBecker I know, see my comment under the original post :) – chrysante Mar 18 '23 at 13:17