Printing out objects in an array

I have to write a code for an assignment that requires me to write an array in main that will hold the objects in my class and print them out. I know that I need a for loop and everything like that but I dont know how to get it to print out without syntax error.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  class Rover{
    
private:
    
    string name;
    int xpos;
    int ypos;
    string direction; //Use Cardinal Directions (N,S,E,W)
    int speed; //(0-5 m/sec)
    
public:
    //Constructors
    Rover();
    Rover(string,int,int,string,int);
    
    //Get functions
    string getName()const;
    int getXpos()const;
    int getYpos()const;
    string getDirect()const;
    int getSpeed()const;
    void getRoverData();
    
    //Set functions
    void setName(string);
    void setXpos(int);
    void setYpos(int);
    void setDirect(string);
    void setSpeed(int);
 };   
    //Constructor function
    Rover::Rover()
    {
        xpos=0;
        ypos=0;
        direction="N";
        speed=0;
    }
 
Rover::Rover(string nme,int xp, int yp, string dir, int sp) :
   name(nme), xpos(xp), ypos(yp), direction(dir), speed(sp) 
{

}
Rover readRover()
{
   string nme;
   int xp;
   int yp;
   string dir;
   int sp;
   
   cout<< "Please enter the Name of the Rover: ";
   cin>> nme;

   cout << "Please enter the starting X-position: ";
   cin >> xp;

   cout << "Please enter the starting Y-position: ";
   cin >> yp;

   cout << "Please enter the starting direction (N,S,E,W): ";
   cin >> dir;

   cout << "Please enter the starting speed (0-5): ";
   cin >> sp;

   // Construct an object with the user input data and return it.
   return Rover(nme, xp, yp, dir, sp);  
}
   
    //Getter functions
    string Rover::getName()const
    {
        return name;
    }
    
    int Rover::getXpos()const
    {
        return xpos;
    }
    
    int Rover::getYpos()const
    {
        return ypos;
    }
    
    string Rover::getDirect()const
    {
        return direction;
    }
    
    int Rover::getSpeed()const
    {
        return speed;
    }
    
    void Rover::getRoverData()
    {
        cout<<"Rover name: "<<name<<endl;
        cout<<"X-Position: "<<xpos<<endl;
        cout<<"Y-Position: "<<ypos<<endl;
        cout<<"Direction: "<<direction<<endl;
        cout<<"Speed: "<<speed<<endl;
    }
    
    //Setter functions
    void Rover::setName(string nme)
    {
        cin>>nme;
        name=nme;
    }
    
    void Rover::setXpos(int x)
    {
        cin>>x;
        xpos=x;
    }
    
    void Rover::setYpos(int y)
    {
        cin>>y;
        ypos=y;
    }
    
    void Rover::setDirect(string direct)
    {
        cin>>direct;
        direction=direct;
    }

    void Rover::setSpeed(int spd)
    {
        cin>> spd;
        speed=spd;
    }


int main(int argc, char** argv) {
    
    int MAX=5;
    
    Rover r1=readRover();
    Rover r2=readRover();
    Rover r3=readRover();
    Rover r4=readRover();
    Rover r5=readRover();
    r1.getRoverData();
    
   int values[MAX];
    values[0]=Rover r1;
    values[1]=Rover r2;
    values[2]=Rover r3;
    values[3]=Rover r3;
    values[4]=Rover r4;
 
    for(int i=0;i<MAX;i++)
    {
        cout<<values[i];
    }
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(int argc, char** argv) {
    
    int MAX=5;
    
    Rover r1=readRover();
    Rover r2=readRover();
    Rover r3=readRover();
    Rover r4=readRover();
    Rover r5=readRover();
    r1.getRoverData();
    
   int values[MAX];
    values[0]=Rover r1;
    values[1]=Rover r2;
    values[2]=Rover r3;
    values[3]=Rover r3;
    values[4]=Rover r4;
 
    for(int i=0;i<MAX;i++)
    {
        cout<<values[i];
    }
    return 0;
}


I do see a problem there.
Your array is of type int, but Rover is its own class. We can't make that assignment. However, Rover has member variables that are of an integer type. You could assign those values to your array.

We can make arrays of any type (well, almost), so you might want to try creating an array of Rovers.
So how would i go about making an array of the rover information. How would my code change? Im just kind of confused on that whole part
Well, just like you make an array of integers using int values[MAX];, you can make an array of Rovers by indicating the type when you make the array. Rover myRoverArray[MAX];

Now you can loop through it in your for-loop. But you'll still have to call "getRoverData" for each of them, and "readRover" for each of them.
As JayZom pointed out, an array of Rovers is just like an array of anything else.

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
int main(int argc, char** argv) 
{   const int MAX=5;
    Rover rovers[MAX];
    
    rovers[0] = readRover();
    rovers[1] = readRover();
    rovers[2] = readRover();
    rovers[3] = readRover();
    rovers[4] = readRover();
 
    for(int i=0;i<MAX;i++)
    {   rovers[i].getRoverData();
    }
    return 0;
}

Last edited on
Topic archived. No new replies allowed.