C++ Expression must have constant value

G V
#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;

template <class T>
void sort(int n, T a[]){
       for(i=0;i<n-1;i++){
          for(j=i;j<n;j++){
               if(a[i] > a[j]){
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
               }
           }
     }
}


void main(){
    int size;
    cout<<" Please input the amount of numbers you would like to sort"<<endl;
    cin>>size;
    int Amta[size];
    for(int i=0; i<size; i++){
        cout<<"Please enter the "<<size+1<< "number";
        cin>>Amta[i];
    }
    Sleep(100000);
}

I am trying to get the how many numbers the user would like to input from the user and store it in the variable size.

But when I initialize array Amta[size] I get the following compile errors:

Expression must have constant value

and

C2057: expected constant expression" compile error.

Aaron Golden

You can't enter a non-constant value between the brackets when you declare your array:

int Amta[size];

Since you're getting size from the user, the compiler can't tell ahead of time how much memory it needs for Amta. The easiest thing to do here (especially for an exercise) is to just choose a relatively large value and make that the constant allocation, like:

int Amta[1024];

And then if you want to be careful (and you should) you can check if (size > 1024) and print an error if the user wants a size that's beyond the pre-allocated bounds.

If you want to get fancy, you can define Amta with no pre-set size, like int *Amta; and then you allocate it later with malloc:

Amta = (int *)malloc(sizeof(int) * size);

Then you must also free Amta later, when you're done with it:

free(Amta);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++ expression must have a constant value

From Dev

Visual C++ Expression must have a constant value

From Dev

Array definition - Expression must have a constant value

From Dev

Intellisense expression must have a constant value

From Dev

Expression must have constant value Eigen matrix

From Dev

Error: expression must have constant value. For a Clearly Constant Value

From Dev

"function call must have a constant value in a constant expression"

From Dev

Compiler showing "Expression must have a constant value" in VS2015 (C Programming)

From Dev

Error[Pe028]: expression must have a constant value

From Dev

2-D Array - Expression must have a constant value error

From Dev

Expression must have a constant value error in an array with the size input by user

From Dev

block and expression must have a const value error

From Dev

The value for annotation attribute FindBy.xpath must be a constant expression

From Dev

The value for annotation attribute FindBy.xpath must be a constant expression

From Dev

Expression must have a pointer to object type in C

From Dev

Case expressions must be constant expression

From Dev

Case expressions must be constant expression

From Dev

c tree structure expression must have a class type

From Dev

c tree structure expression must have a class type

From Dev

JPA/HIBERNATE: Value must be a constant

From Dev

BuildConfig: Attribute value must be constant

From Dev

Attribute Value must be constant in @Retryable

From Dev

expression must be a modifiable value

From Dev

Expression must be an L value

From Dev

Index expression must be constant - WebGL/GLSL error

From Dev

Verilog range must be bounded by constant expression

From Dev

Custom attribute: An attribute argument must be a constant expression

From Dev

C, Expression must be a modifiable lvalue (changing the value of a struct's member)

From Dev

expression must be a modifiable L value

Related Related

  1. 1

    c++ expression must have a constant value

  2. 2

    Visual C++ Expression must have a constant value

  3. 3

    Array definition - Expression must have a constant value

  4. 4

    Intellisense expression must have a constant value

  5. 5

    Expression must have constant value Eigen matrix

  6. 6

    Error: expression must have constant value. For a Clearly Constant Value

  7. 7

    "function call must have a constant value in a constant expression"

  8. 8

    Compiler showing "Expression must have a constant value" in VS2015 (C Programming)

  9. 9

    Error[Pe028]: expression must have a constant value

  10. 10

    2-D Array - Expression must have a constant value error

  11. 11

    Expression must have a constant value error in an array with the size input by user

  12. 12

    block and expression must have a const value error

  13. 13

    The value for annotation attribute FindBy.xpath must be a constant expression

  14. 14

    The value for annotation attribute FindBy.xpath must be a constant expression

  15. 15

    Expression must have a pointer to object type in C

  16. 16

    Case expressions must be constant expression

  17. 17

    Case expressions must be constant expression

  18. 18

    c tree structure expression must have a class type

  19. 19

    c tree structure expression must have a class type

  20. 20

    JPA/HIBERNATE: Value must be a constant

  21. 21

    BuildConfig: Attribute value must be constant

  22. 22

    Attribute Value must be constant in @Retryable

  23. 23

    expression must be a modifiable value

  24. 24

    Expression must be an L value

  25. 25

    Index expression must be constant - WebGL/GLSL error

  26. 26

    Verilog range must be bounded by constant expression

  27. 27

    Custom attribute: An attribute argument must be a constant expression

  28. 28

    C, Expression must be a modifiable lvalue (changing the value of a struct's member)

  29. 29

    expression must be a modifiable L value

HotTag

Archive