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?
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[],
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
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 = newint [ 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;