Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple Array Question - Help

Reply
Thread Tools

Simple Array Question - Help

 
 
Ali
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      08-26-2004
In article <(E-Mail Removed)>,
Ali <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      08-26-2004
In article <(E-Mail Removed)>,
Ali <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
simple, simple array question Peter Bailey Ruby 7 04-08-2008 01:54 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments