data structure problem

Those some parts of my program. I write the problem at the buttom
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
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 


my function
1
2
3
4
5
6
7
8
9
10
11
12
13
void listModified()
{
	warehouse car[2];


for (i=0;i<2;i++)
	{
		car[i].printModified();
		//cout<<"COUT: "<<car[i].checkModified()<<endl;
	}


}


i noticed that i must have warehouse car[2]; to every function i write elsewhere i get the following errors

error C2065: 'car' : undeclared identifier
and
error C2228: left of '.printModified' must have class/struct/union

the problem is that the values change.
any way to fix this ?

Thanks for your time!
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.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int i=0;
int total=0;
int check=1;

void listModified();

class warehouse
{
public:
    warehouse()
    {
    PRVmodified=true;
    }

    void printModified();
    void checkModified();
    void inputCar(char car[20],bool modified);
    void showCar();
private:
    char PRVcar[20];
    bool PRVmodified;

};
void warehouse::inputCar(char car[20],bool modified)
{
    strcpy(PRVcar,car);
    PRVmodified=modified;
}

void warehouse::showCar()
{
    cout<<"car model is: "<<PRVcar<<endl;
    cout<<"car is modified: "<<PRVmodified<<endl<<endl;
}
void warehouse::checkModified()
{
if (PRVmodified == 1)
{
            ++total;
            
}

}
void countModified()
{
    warehouse car[2];
    
    for (i=0;i<2;i++)
    {
        car[i].checkModified();
    }
    cout<<"The total cars modified: "<<total<<endl;
}

void warehouse::printModified()
{
if (PRVmodified == 1)
{
    cout<<"the car that is modified: "<<endl;
            
}
}
void listModified()
{
for (i=0;i<2;i++)
    car[i].printModified();

}
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 


Thanks for your time !
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?

Again thanks for your help.
It works with global variable, but i wanted to make it work with passigf the arguements..
I fix it and work with arguements now.
Just have to change

countModified(car);
and
void listModified(warehouse ArrayOfStruct[]) ;

Topic solved!

Thanks for the help.
Topic archived. No new replies allowed.