| | |
array size declaration after inputs
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 62
Reputation:
Solved Threads: 0
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?
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?
c++ Syntax (Toggle Plain Text)
#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.
Chris
You should be using dynamic allocation when dealing with creating an array of size N where N is specified by user input.
C++ Syntax (Toggle Plain Text)
int *example = new int[N]; ... delete [] example;
Chris
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- help with reaction on this... (Java)
- math program (C)
Other Threads in the C++ Forum
- Previous Thread: A simple GetOpenFileName example
- Next Thread: ftell() is returning the wrong position of file why?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





