May 10, 2013 at 12:31am UTC
hello,
I've been working on a project for a couple of days, and am stuggling with arrays. My Prof has not gotten back to me, so I was hoping someone here could help.
anyways, the project is designing a mars rover simulator. I'm having trouble displaying all the rover data i have entered.(getRoverData) is not properly and I get this error message I have never seen before after the run is finished. any help is greatly appreciated! here's my code:
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
class Rover {
private:
string rover_name;
int x_position;
int y_position;
string direction;
int speed;
public:
Rover();
void setName(string rover_name);
void setX(int x_position);
void setY(int y_position);
void setDirection(string direction);
void setSpeed(int speed);
string getName(){
return rover_name;
}
int getX(){
return x_position;
}
int getY(){
y_position;
}
string getDirection(){
return direction;
}
int getSpeed(){
return speed;
}
string getRoverData(){
cout << rover_name << endl;
cout << x_position << endl;
cout << y_position << endl;
cout << direction << endl;
cout << speed << endl;
}
Rover(string name, int x, int y, string Dir, int Speed){
this->rover_name = name;
this->x_position = x;
this->y_position = y;
this->direction = Dir;
this->speed = Speed;
}
};
Rover::Rover(){
this->rover_name = "default";
this->x_position = 0;
this->y_position = 0;
this->direction = "N";
this->speed = 0;
};
int main(int argc, char** argv) {
int number_of_rovers;
int tempx, tempy, tempSpeed;
string tempName, tempDir;
Rover roverarray[number_of_rovers];
cout << "How many Rover(s) do you want to create: " << endl;
cin >> number_of_rovers;
int i;
for (i=0; i<number_of_rovers; i++){
cout << "What is the Rover's name: " <<endl;
cin >> tempName;
cout << "What is the Rover's x-position: " <<endl;
cin >> tempx;
cout << "What is the Rover's y-position: " <<endl;
cin >> tempy;
do {
cout << "Enter N, S, E, or W." << endl;
cout << "What is the Rover's direction: " <<endl;
cin >> tempDir;
} while
(!(tempDir=="N"|| tempDir=="S"|| tempDir=="E" || tempDir=="W"));
do {
cout << "Enter a speed of 1-5." << endl;
cout << "What is the Rover's speed: " << endl;
cin >> tempSpeed;
} while
(!(tempSpeed==1 || tempSpeed==2 || tempSpeed==3 || tempSpeed==4 || tempSpeed==5));
}
roverarray[i] = Rover(tempName, tempx, tempy, tempDir, tempSpeed);
for (i=0; i<number_of_rovers; i++){
cout << roverarray[i].getRoverData();
}
return 0;
};
May 10, 2013 at 3:15am UTC
changed a few things
add <string>
1 2 3 4 5 6 7
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
change to void
1 2 3 4 5 6 7 8
void getRoverData(){
cout << rover_name << endl;
cout << x_position << endl;
cout << y_position << endl;
cout << direction << endl;
cout << speed << endl;
}
change to dynamic and after input
1 2 3
cout << "How many Rover(s) do you want to create: " << endl;
cin >> number_of_rovers;
Rover *roverarray = new Rover[number_of_rovers];
change the ending of the main for loop to after you call rover to store into roverarray
1 2 3 4 5 6 7 8 9 10 11 12 13
} while
(!(tempSpeed==1 || tempSpeed==2 || tempSpeed==3 || tempSpeed==4 || tempSpeed==5));
// move from here
roverarray[i] = Rover(tempName, tempx, tempy, tempDir, tempSpeed);
} // <--to here
for (i=0; i<number_of_rovers; i++){
roverarray[i].getRoverData();
}
cin.ignore(2);
delete [] roverarray;
return 0;
this works for me
Last edited on May 10, 2013 at 3:31am UTC
May 10, 2013 at 5:59am UTC
I made the changes and it won't compile... also I don't understand why you changed getRoverData to a void... I thought void functions don't return values, but I'm trying to return all the data entered into the array....right?
May 10, 2013 at 2:32pm UTC
In getRoverData, you use cout. This will display what you want instantly, so you don't need to return anything.
What system are you using? I use windows, MVSE2k12 and have no issues.
Whats the error you are getting?
May 10, 2013 at 3:09pm UTC
I'm using netbeans. on this line, roverarray[i] = Rover(tempName, tempx, tempy, tempDir, tempSpeed);
I'm getting unexpected token. here's the whole code changed.
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
class Rover {
private:
string rover_name;
int x_position;
int y_position;
string direction;
int speed;
public:
Rover();
void setName(string rover_name);
void setX(int x_position);
void setY(int y_position);
void setDirection(string direction);
void setSpeed(int speed);
string getName(){
return rover_name;
}
int getX(){
return x_position;
}
int getY(){
y_position;
}
string getDirection(){
return direction;
}
int getSpeed(){
return speed;
}
void getRoverData(){
cout << rover_name << endl;
cout << x_position << endl;
cout << y_position << endl;
cout << direction << endl;
cout << speed << endl;
}
Rover(string name, int x, int y, string Dir, int Speed){
this->rover_name = name;
this->x_position = x;
this->y_position = y;
this->direction = Dir;
this->speed = Speed;
}
};
Rover::Rover(){
this->rover_name = "default";
this->x_position = 0;
this->y_position = 0;
this->direction = "N";
this->speed = 0;
};
int main(int argc, char** argv) {
int number_of_rovers;
int tempx, tempy, tempSpeed;
string tempName, tempDir;
Rover roverarray[number_of_rovers];
cout << "How many Rover(s) do you want to create: " << endl;
cin >> number_of_rovers;
Rover roverarray = new Rover[number_of_rovers];
int i;
for (i=0; i<number_of_rovers; i++){
cout << "What is the Rover's name: " <<endl;
cin >> tempName;
cout << "What is the Rover's x-position: " <<endl;
cin >> tempx;
cout << "What is the Rover's y-position: " <<endl;
cin >> tempy;
do {
cout << "Enter N, S, E, or W." << endl;
cout << "What is the Rover's direction: " <<endl;
cin >> tempDir;
} while
(!(tempDir=="N"|| tempDir=="S"|| tempDir=="E" || tempDir=="W"));
do {
cout << "Enter a speed of 1-5." << endl;
cout << "What is the Rover's speed: " << endl;
cin >> tempSpeed;
} while
roverarray[i] = Rover(tempName, tempx, tempy, tempDir, tempSpeed);
} (!(tempSpeed==1 || tempSpeed==2 || tempSpeed==3 || tempSpeed==4 || tempSpeed==5));
for (i=0; i<number_of_rovers; i++){
cout << roverarray[i].getRoverData();
}
cin.ignore(2);
delete[] roverarray;
return 0;
};
May 10, 2013 at 3:54pm UTC
EDIT: so there were a few things that were a little off so I'll post full code
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
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
class Rover {
private :
string rover_name;
int x_position;
int y_position;
string direction;
int speed;
public :
Rover();
void setName(string rover_name);
void setX(int x_position);
void setY(int y_position);
void setDirection(string direction);
void setSpeed(int speed);
string getName(){
return rover_name;
}
int getX(){
return x_position;
}
int getY(){
y_position;
}
string getDirection(){
return direction;
}
int getSpeed(){
return speed;
}
void getRoverData(){
cout << rover_name << endl;
cout << x_position << endl;
cout << y_position << endl;
cout << direction << endl;
cout << speed << endl;
}
Rover(string name, int x, int y, string Dir, int Speed){
this ->rover_name = name;
this ->x_position = x;
this ->y_position = y;
this ->direction = Dir;
this ->speed = Speed;
}
};
Rover::Rover(){
this ->rover_name = "default" ;
this ->x_position = 0;
this ->y_position = 0;
this ->direction = "N" ;
this ->speed = 0;
};
int main(int argc, char ** argv) {
int number_of_rovers;
int tempx, tempy, tempSpeed;
string tempName, tempDir;
// don't declare any array here
cout << "How many Rover(s) do you want to create: " << endl;
cin >> number_of_rovers;
Rover *roverarray = new Rover[number_of_rovers]; // declare array here, but as a pointer *
for (int i=0; i<number_of_rovers; i++) {
cout << "What is the Rover's name: " <<endl;
cin >> tempName;
cout << "What is the Rover's x-position: " <<endl;
cin >> tempx;
cout << "What is the Rover's y-position: " <<endl;
cin >> tempy;
do {
cout << "Enter N, S, E, or W." << endl;
cout << "What is the Rover's direction: " <<endl;
cin >> tempDir;
} while (!(tempDir=="N" || tempDir=="S" || tempDir=="E" || tempDir=="W" ));
do {
cout << "Enter a speed of 1-5." << endl;
cout << "What is the Rover's speed: " << endl;
cin >> tempSpeed;
} while (!(tempSpeed==1 || tempSpeed==2 || tempSpeed==3 || tempSpeed==4 || tempSpeed==5));// this goes on the do-while loop
// need this before the end of the for loop
roverarray[i] = Rover(tempName, tempx, tempy, tempDir, tempSpeed);
}// end for
for (int i=0; i<number_of_rovers; i++)
roverarray[i].getRoverData(); // don't need cout
cin.ignore(2);
delete [] roverarray;
return 0;
}
Last edited on May 10, 2013 at 5:43pm UTC
May 10, 2013 at 11:32pm UTC
thanks a ton! im close but there is one part I need to change but dont know how... getRoverData has to display with labels of each variable after each rover, not all data at the end... the a function called DisplayAllRoverData displays all the data at the end of the run with labels in a "tabular"format per the assignment. any ideas?? you've been very helpful thus far!!
May 11, 2013 at 2:55am UTC
nevermind! I figured it out!