By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Simple Array Question - Help

P: n/a
Ali
Hi,

I want to create an array, whose size should be entered by the user,
instead of me hardcoding it on the code.

The following is what i want to do:

#include <iostream.h>

int main()
{
int numElements=0;
cin>>numElements;

int Array[numElements];

//do something

return 0;
}

The compiler gives an error asking for a constant, such as int
Array[5], etc.

How do i solve the problem, if i want to let the user choose the Array
Size?

Thanks,

Ali
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ali wrote:
Hi,

I want to create an array, whose size should be entered by the user,
instead of me hardcoding it on the code.

The following is what i want to do:

#include <iostream.h>

#include <iostream>

Not needed in the code below.

int main()
{
int numElements=0;
cin>>numElements;

int Array[numElements];

int *Array=new int[numElements];


//do something

delete[] Array;
Or better use std::vector.


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2

P: n/a
Ali wrote:
I want to create an array, whose size should be entered by the user,
instead of me hardcoding it on the code.

The following is what i want to do:

#include <iostream.h>

int main()
{
int numElements=0;
cin>>numElements;

int Array[numElements];

//do something

return 0;
}

The compiler gives an error asking for a constant, such as int
Array[5], etc.

How do i solve the problem, if i want to let the user choose the Array
Size?


You would have to create the array dynamically. Read your favourite C++
book on 'new[]' and 'delete[]'.

Victor
Jul 22 '05 #3

P: n/a
In article <4k********************************@4ax.com>,
Ali <al*@ali.net> wrote:

The following is what i want to do:

#include <iostream.h>

int main()
{
int numElements=0;
cin>>numElements;

int Array[numElements];

//do something

return 0;
}


Use a vector instead.

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
int numElements = 0;
cin >> numElements;

vector<int> Array[numElements];

// do something

return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

P: n/a
In article <4k********************************@4ax.com>,
Ali <al*@ali.net> wrote:

The following is what i want to do:

#include <iostream.h>

int main()
{
int numElements=0;
cin>>numElements;

int Array[numElements];

//do something

return 0;
}


Use a vector instead.

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
int numElements = 0;
cin >> numElements;

vector<int> Array(numElements); // note parentheses not brackets

// do something

return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.