![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
A problem
Hi all,
I'm a beginner of C++. I'd like to ask how I could input an integer n, and after running the program, return an array with length n. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
void main( )
{
int n = 0;
cin >> n;
char *ptr = new char( n );
return ptr; // WTF?
}Just for the record, that will not return the array though... applications only return numeric values... not arrays. Perhaps you mean have a function which returns the array? Well that demonstrates how to do it, just remember to release you pointer when you are done with it.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
In fact I just want to input an integer n and then I can form an array with length n which can be used in the main program.
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You can't define an array with a variable length. You can, however, use memory allocation to do the same thing:
int i; cin >> i; int *array = new int[i]; At the end of the program, you then have to free the memory. Use: delete [] array; You should really know how to work pointers before you mess with this though. |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4
![]() |
Quote:
haha beat u by 10 seconds or so
__________________
... |
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Heh. Forgot about it until I'd posted.
|
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Heh. Guess you guys are correct. I've been getting too used to Java.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Thanks all for replying. But if I want to creat a two-dimensional array, how could I modify the code?
cin >> n; double *array = new array[n][n]; delete array; It seems that it doesn't work... |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 4
![]() |
Quote:
#2 u need a double pointer, I believe ** (not so sure about this but i think thats what u need. I havent used them that often.) #3 your delete needs brackets [][]. Delete alone is used for a single variable. Arrays are different. cin >> n; double **array = new double[n][n]; delete array[][];
__________________
... |
|
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Jan 2005
Posts: 21
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|