Why won't this let me choose my own array size?

Queue line[qs_pair] is what i'm concern about. As you can see, I have the user input from the terminal the size of the array. But when I compile it, it gives me an error on visual studio.

line unknown size
cannot allocate an array of constant size 0

it works on unix, but does not work on visual studio. Anyone has any idea why?

#include <iostream>
#include "queue.h"
#include <iomanip>
using namespace std;

int get_lowest(Queue [], int);

int main()
{
        int qs_pair = 10, maxtran, prob, dur, seed;
        int count(0);                    // The number of customers served
        int entry_time(0);               // When each served customer arrived
        int wait_sum(0);                 // Sum of waiting times
        int server_count(0);             // count the server;

        cout << "how many queue/server pair(s)? ";
        cin >> qs_pair;
        cout << "\nthe max time for customer transaction --> ";
        cin >> maxtran;
        cout << "\nthe probability a customer will arrive --> ";
        cin >> prob;
        cout << "\nduration of simulation(at least 1) --> ";
        cin >> dur;
        cout << "\nduration of simulation(at least 1) --> ";
        cin >> dur;
        cout << "\nenter an integer seed --> ";
        cin >> seed;

        srand(seed);
        Queue line[qs_pair];
        int trans_time[qs_pair]; //time remaining in a transaction

        for(int i = 0; i < qs_pair; i++)
        {
                trans_time[i] = 0;
        }

        int lowest;
        int trans_time_c; // trans_time counter

        for(int time = 0; time < dur; time++)
        {
                if(rand() % 100 < prob)
                {
                        lowest = get_lowest(line, qs_pair);

                        lowest = get_lowest(line, qs_pair);

                        line[lowest].enqueue(time);
                }

                //good above

                for(trans_time_c; trans_time_c < qs_pair; trans_time_c++)
                {
                        if(trans_time[trans_time_c] == 0)
                        {
                                if(line[trans_time_c].return_index() != 0)
                                {
                                        entry_time = line[trans_time_c].dequeue();
                                        wait_sum += (time - entry_time);
                                        ++count;
                                        trans_time[trans_time_c] = (rand() % maxtran) + 1;
                                }
                        }
                        else
                        {
                                trans_time[trans_time_c] -= 1;
                        }

                }
        }


}

int get_lowest(Queue line[], int qs_pair)
{
        int temp = line[0].return_index();
        int temp_i = 0;

        for(int i = 0; i < qs_pair; i++)
        {
                if(line[i].return_index() == 0)
                        return i;
                else if((i == qs_pair - 1) && line[qs_pair - 1].return_index() < temp)
                {
                        temp = line[qs_pair - 1].return_index();
                        temp_i = qs_pair - 1;
                }
                else
				{
             
                        if(line[i].return_index() > temp)
                        {

                        }
                        else
                        {
                                temp = line[i].return_index();
                                temp_i = i;
                        }
                }
        }
        return temp_i;
}

The size of arrays are resolved at compile time, when qs_pair is 10, thus when you enter more than 10 you will get overflow errors.

You should be using dynamic allocation when dealing with creating an array of size N where N is specified by user input.

int *example = new int[N];
...
delete [] example;

Chris

This article has been dead for over six months. Start a new discussion instead.