void main()
{
string option;
warehouse car[2];
car[0].inputCar("lamborgini",true);
car[0].showCar();
car[1].inputCar("ferrari",false);
car[1].showCar();
//display the choices of the user
cout<<"a) Show number of modified"<<endl;
cout<<"b) Show list of modified"<<endl;
cin>> option;
switch (option[0])
{
case'a':
countModified();
break;
case'b':
listModified();
break;
}
}//end main
warehouse car[2]must be declared globally. i.e. visible to all functions, or passed as an argument to countModified() and listModified().
Declaring it inside each function is creates different instances of the array. If you have tried to run your program, you would find prints two blank lines because the array inside listModified() has not been initialized.
You can declare it as a global variable (outside main)
You can pass as a parameter to all the function
1 2
void listModified(warehouse *begin, warehouse *end); //pointers in range [)
void listModified(warehouse *car, int n); //knowing the number of instances
This way your function will work with different sizes of the array.
You can create a collection class that implements the functions as methods, and has the array as member
I tried to declare it globally but again is not working.
Also i tried with the parameters but again something is wrong. Am not sure if i pass them correct though.
Here is my source code if you can trace the mistake.
ne555 showed you how to pass warehouse as an argument to countModified() and listModified(). This is the better approach.
Or to make it global, move warehouse car[2] from inside main to just after the declaration of the class. You also need to remove warehouse car[2] from countModified(). In the code you posted, you should be getting compile errors because warehouse car[2] is not visible to listModified().
Thanks a lot AbstractionAnon and ne555.
Finally i make it work with making it global.
I tried to pass the arguements but i must do something wrong. I Read about pass by value or reference but still didn`t understand. Can you show me the exact changes to lines i have to do?