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
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.
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.
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;
}