473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Simple Array Question - Help

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
4 965
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Clark | last post by:
Dear All I need to do something very simple with Javascript and have been looking all over the web for two days for a suitable script. I'm the kind of person who learns by seeing how it's done....
3
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in...
4
by: Owen Parker | last post by:
Hi all I am not a java programmer but i can hack at it a bit. I am trying to allow a user to define the text in a simple javascript text scroller. The data is stored as multiple records in a...
6
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
6
by: Ronald Raygun | last post by:
I want to be able to randomly select the following from an array: 1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to...
3
by: yottabyte | last post by:
Hey bytes, you may or may not remember but last time I was here a few months ago I got some help with making a hangman program which went well. Now I'm still doing okay in Java this year but I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.