calling functions with arrays

Getting an error under the main function by int car_list[]. I'm aware its incomplete type and I have no specified the size of the array. But I want to call that function array_carsold and store it under this. How can I possibily do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 void array_carssold(int&cars[], int size)
{
	int index;
	cout<<"\n\n\tTotal Car Sold: ";
	for (index=0;index < size;index++)
	{
		cin>>cars[index];
	
	}
	

}
int _tmain(int argc, _TCHAR* argv[])
{
	//Call function: Display
	int num_dealers;
	display (num_dealers);

	//Call functoin: User will enter the amount of cars they sold
	int carlist[]
	array_carssold(carlist[],
It's better if you use vector in this case for c++.
Take a look at std::vector.
Swejjnc, the problem is that we have not learned about vectors on class. We are learning about arrays right now :(
Knock knock?
void array_carssold(int&cars[], int size)

should be :

void array_carssold( int cars[], int size )

-*-*-*-*
1
2
3
4
int carlist[] // <-- the size of the array must be determined, and you are
              //missing a semicolon
array_carssold(carlist[], // <- remove '[]', and you are missing the second argument
                                   // which is the size of the array, and ')' and a semicolon 
Last edited on
Thank you shadown fiend. This is what I have done so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

void display (int &numberofdealers)
{
	cout<<"Output total number of cars sold by the person and output the salesperson selling\nmaximum amount of cars: ";
	cout <<"\n\n\tHow many dealers do you have in your company: ";
	cin>>numberofdealers;




}

//Function: Store the number of cars sold by each dealer. Dealer will enter the total amount of cars they sold

void array_carssold(int cars[],int size)
{
	int index;
	cout<<"\n\n\tTotal Car Sold: ";
	for (index=0;index < size;index++)
	{
		cin>>cars[index];
	
	}
	

}

void fileopen (ifstream& indata, ofstream &outdata)
{
	indata.open("Dealership.txt");
	outdata.open("Dealership.out");


}
int _tmain(int argc, _TCHAR* argv[])
{

	//Function Call: Open the output and inptu files
	ifstream in;
	ofstream out;
	fileopen (in,out);

	
	//Call function: Display
	int num_dealers;
	display(num_dealers);
	cout<<"\n\nThere are " << num_dealers;

	//Call functoin: User will enter the amount of cars they sold
	int array_size=num_dealers;
	int carlist[array_size]
	
I have tried to store the value, I get from num_dealers to array-size; however, I get an error under int carlist[array_szie] saying expression does not have a constant value. All I want this program to do is ask the user how many dealers I have in the company and the number they stores would be the array size. :)
Just read my book and it said the most likeliest scenario would be to use pointers; however, i'm not up to that level yet. Thank you for your help though :)
that is because an array size can only be determined at compile time :

Let's say i have a program :
1
2
3
4
5
6
7
8
int n;
cout << "How many numbers you would like to enter : "
cin >> n;

int array[ n ]; // error ! n's value is undetermined at compile time, it is only 
                   // determined at runtime, an error is raised by the compiler

for ( int i = 0; i < n; i ++ ) { ... }


in order to solve this kind of memory allocation problem, you should use additional c++ feature : dynamic memory allocation :

1
2
3
4
5
6
7
8
9
10
11
12
int n;
cout << "How many numbers you would like to enter : "
cin >> n;

int* pointer = new int [ n ]; // now it's fine

for( int i = 0; i < n; i++ ) { ... }
// ...

/// Important : don't forget to delete the value the pointer points to :

delete [] pointer;


so in your program,

int carlist[array_size]

should be :

1
2
3
4
5
6
7
int* carlist = new int[ array_size ];
// ...
// ...
delete [] carlist;
return 0;

}





http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
I grasped the concept; however, may you explain to me the difference between compile time and runtime?
well, it comes from the word itself, compile time means the time when the program is being compiled and translated into machine readable form.

runtime, on the other hand means the time when the program is running

-*-*-*-*

or if you mean compile time and runtime memory allocation in arrays,

compile time allocation means the size of the array is known at compile time
( also called static allocation )

runtime ( or dynamic ) means the size is determined at runtime
Last edited on
Makes much more sense now! Thanks Shadow Fiend
Topic archived. No new replies allowed.