Print all data members from n classes

Hello,
So I am trying to create a program that simulates a bunch of Rovers driving around and hopefully not bumping into each other.
I am sure I will have more questions later, but for now I am just having trouble printing out the initial user input rover data for all rovers. I know and I have issues with my references at the very least. I want to only run the print function n="num" times(the user input number of rovers). It should then go through and print name, x,y,v and direction for all rovers. Any help would be great!
Thanks
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
  #include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

/*
 * 
 */
class Rover
{
    private:
        string name;
        char dir;
        int x;
        int y;
        int v;
    public:  
        Rover();
        void de_constructor();
        void setRov();
        void dis_all_Rov();
        string get_rover_data();
};

int main(int argc, char** argv) {
    int num;
    cout<< "How many Rovers do you want?" <<endl;
    cin >> num;
    Rover pop_rov[num];
    for(int i=0; i<num;i++){
        Rover();
        pop_rov[i].setRov();
        
    }
       
    return 0;
}
Rover::Rover(){//Default constructor
    x=0;
    y=0;
    v=0;
    dir= 'N';
    name= 'Default';
}
void Rover::setRov(){//add specific values to rover
    cout<< "enter rover name: ";
    cin>> name;
    cout << "Enter X-position: ";
    cin >> x;
    cout << "Enter Y-position: ";
    cin >> y;
    cout << "Enter the current direction of the rover(N,S,E,W): ";
    cin>> dir;
    while(dir != 'N' && dir != 'n' && dir != 'S' && dir != 's' && dir != 'e' && dir != 'E' && dir != 'w' && dir != 'W'){
      cout<< "invalid direction: ";
      cin>> dir;
    }
    cout << "Enter the speed(0-5): ";
    cin >> v;
    while(v<0 || v>5){
        cout<<"enter a speed between 0 and 5: ";
        cin >> v;
    }
    
    
}

void Rover::dis_all_Rov(class Rover setRov&, int num&){//prints rover data
    for (int i=0; i<4; num& ){
         
    cout << "Rover name: "<< pop_Rov[i].name << endl;
    cout << "Rover X-position: "<< pop_Rov[i].x << endl;
    cout << "Rover Y-position: "<< pop_Rov[i].y << endl;
    cout << "Rover direction: "<< pop_Rov[i].dir << endl;    
    cout << "Rover speed: "<< pop_Rov[i].v << endl;
}

}
Last edited on
You should read into passing by reference, this code would never work. Test code as you write it, don't just write a whole program which is a disaster and expect it to work.

For instance:
class Rover setRov&
Should be:
Rover* rovers

Then pass the array of rovers to it, then you can do "rovers[1].name", etc.

int num&
Should be:
int num

Then loop through rovers up to this number of elements.

Read up about classes, you're taking a short cut and it's certainly not paid off.

Topics:
Classes
Class constructors
Pass by reference
C arrays
For loops
Instantiating Class Instances

It's very hard to help people when their code contains problems everywhere, as it hasn't been tested as it was developed. People would opt to start again.
Last edited on
Thank you for being blunt. I did start over and now making some progress and have a little better understanding of classes.

It now does its basic function of creating n rovers and displaying their data.
I now need to add a function which moves them around. I created elapsed_time function as a public member within the class(that's why there's no Rover::elapsed_time). When I run I get tons of
"error: expected primary-expression before ‘.’ token" all throughout my function. I have not posted the full code as it is very large. Any help would be great.

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
int elapsed_time(){
 int t;
    cout << "Enter the time elapsed since the last rover was moved(sec)";
    cin >> t;    
    if(Rover.dir=="N" || Rover.dir()=="n"){// if its north, add to the y position
        Rover.y_pos=Rover.y_pos+(Rover.v_cur * t);
    }
    if(Rover.dir=="S" || Rover.dir()=="s"){// if its south, subtract from the y position
        Rover.y_pos=Rover.y_pos-(Rover.v_cur * t);
    }
    if(Rover.dir=="E" || Rover.dir()=="e"){//if its east, add to the y position
        Rover.x_pos=Rover.x_pos+(Rover.v_cur * t);
    }
   if(Rover.dir=="W" || Rover.dir()=="w"){//if its west, subtract from the y position
        Rover.x_pos=Rover.x_pos-(Rover.v_cur * t);
   }
}

};// This is a class function..

{
    
    for(int j=0; j<num; j++){//this is supposed to send all the Rover objects through the elapsed_time function
       pop_rov[j].elapsed_time();
    }
        
    return 0;
}
Topic archived. No new replies allowed.