0

I'm getting errors related to the array size, the errors are:

line 19: expression did not evaluate to a constant <br/>
line 31: array type 'int[*n]' is not assignable <br/>
line 34: array type 'int[*n]' is not assignable <br/>


#include "stdafx.h"
#include "iostream"

int main()
{

    using namespace std;
    int x=0;
    int cst=0;
    int cstF=0;
    int rst=0;
    int n=1;

    cout << "insira o numero de consultas" << endl;
    cin >> n;

    int hora[2 * n];

    for (x = 0; x == 2 * n - 2; x += 2) 
    {
        cout << "insira o horario de inicio" << endl;
        cin >> cst;
        if (hora[x - 2] < cst && hora[x - 1] > cst)
            rst = rst;
        else
        {
           rst = rst + 1;
        }
        hora[x] = cst;
        cout << "insira o horario de termino" << endl;
        cin >> cstF;
        hora[x + 1] = cstF;
    }
    cout << "o numero de consultas possiveis eh: " << rst << endl;
    return 0;
}

I'd really appreciate some light on why I'm getting errors.

dragosht
  • 3,237
  • 2
  • 23
  • 32
  • 2
    Variable length arrays are an extension, not c++ standard. If you need a variable length array use a `std::vector` instead. – πάντα ῥεῖ Mar 10 '16 at 09:16
  • 2
    I think the error messages are quite clear! Your array size needs to be a (compile time) constant, and you give it some value that is only known at run time. Perhaps you are looking for [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)? – BoBTFish Mar 10 '16 at 09:17
  • And a couple of stylistic points to get on top of now, before they are too embedded and hard to un-learn: Standard library headers should be included with `<>`, like `#include ` - this tells the system to look in the correct place, rather than checking your local directory first. Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Mar 10 '16 at 09:19
  • if you want to use an `int` array: `int *hora = new int [2*n];` and at the end of your code: `delete hora;` – Zoltán Mar 10 '16 at 09:23
  • Also, `hora[x - 2]` and `hora[x - 1]` will not work when `x` is 0 or 1. – Bo Persson Mar 10 '16 at 09:59
  • so i've made some small changes, removed the `using namespace`,`endl`, but now after my code reads `n` he goes to `return 0;`. i've also changed `int hora[2 * n];` to `std::vector hora(n);`. @BoPersson i was thinking about that, but i thought that since those elemends didn't existed they would be 0. back to thinking – Lucas Pontes Mar 10 '16 at 10:05

1 Answers1

0

you cannot statically allocate an array using a variable as size;

int n;
int hora[n]; //won't compile

you can either use dynamic memory allocation:

int n;
int* hora = malloc(sizeof(int)*n);

of use a const variable:

const int n = 10;
int hora[n];

if you insist on reading the size from console(or anywhere else) at runtime, and statically allocate memory, you can do:

int n;
cin >> n;
const int size = n;
int hora[size];
Pandrei
  • 4,843
  • 3
  • 27
  • 44
  • your last example isn't valid. `n` needs to be a compile-time constant expression in order to be used as the declaration size of an array. See http://stackoverflow.com/a/34696833/2805305 – bolov Mar 10 '16 at 10:39
  • `malloc` in a C++ program? When you can simply use `std::vector`? – PaulMcKenzie Mar 10 '16 at 10:46
  • @PaulMcKenzie the question is tagged c++, but the code is more c-style; hence the c-style answer – Pandrei Mar 10 '16 at 10:59