Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-20-2007, 12:10 PM
dksmarte dksmarte is offline
Newbie
 
Join Date: Mar 2007
Posts: 2
Rep Power: 0
dksmarte is on a distinguished road
Default C++ Array Problem

I'm trying to create an array of varying length depending on the input of the user as demonstrated in the following code:

Code:
#include <iostream.h>

int main(void)
{
	int size;
	cout << "Enter array size: ";
	cin >> size;
	
	int data [size];
	
	return 0;
}
When I compile, I get the following error:

Quote:
Error E2313 test.cpp 9: Constant expression required in function main()
Can anyone enlighten me on how to circumvent this error?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-20-2007, 12:19 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,405
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

Your problem is just what the error says: you are not allowed to dynamically determine the size of an array that way. You will either have to use the new[] operator and a pointer, or use a container class from the STL.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2007, 05:06 PM
bobwrit bobwrit is offline
Newbie
 
Join Date: Apr 2007
Posts: 28
Rep Power: 7
bobwrit is on a distinguished road
Default

I've tried this a few times and it hasn't worked for me so in my opinion you cant declare a array of variable size.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2007, 11:19 PM
Deathcry's Avatar   
Deathcry Deathcry is offline
Learning Programmer
 
Join Date: Feb 2007
Posts: 69
Rep Power: 7
Deathcry is on a distinguished road
Default

Quote:
Originally Posted by dksmarte View Post
I'm trying to create an array of varying length depending on the input of the user as demonstrated in the following code:

Code:
#include <iostream.h>

int main(void)
{
	int size;
	cout << "Enter array size: ";
	cin >> size;
	
	int data [size];
	
	return 0;
}
When I compile, I get the following error:



Can anyone enlighten me on how to circumvent this error?
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int size;
cout << "Enter array size: ";
cin >> size;

int data [size];

return 0;
}

ur main is all messed up. try that...
__________________
the code is with you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-13-2007, 12:52 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,651
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I would create the array dynamically, using the new-operator.
Code:
#include <iostream>

int main()
{
	int *arr;
	int  arr_Size;
	
	std::cout << "Array size: ";
	std::cin  >> arr_Size;
	
	arr = new int[arr_Size];
	
	// Use the array in different ways
	
	delete [] arr;
	return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Peculiar UI Problem Needs Tackling adriyel C# Programming 2 04-06-2008 08:46 AM
fread into array position kenna C and C++ 0 08-17-2007 09:03 AM
Having a problem with a relatively easy problem... Yaglana Java Help 0 04-26-2007 08:33 PM
Python 2D array question annannienann Python 3 04-23-2007 05:36 PM
Directory Size John PHP Forum 5 08-06-2006 02:02 PM


All times are GMT -5. The time now is 09:22 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads