I have created a class that allows me to create and move "space rovers" on an imaginary grid. For an assignment, I need to make it so the rover can output to a file every time it makes a move or turns. I figured that part out, but now I need to make it so MULTIPLE rovers can be created and moved around at the same time, all of them writing to a file when perform an action. I'm sure I can get credit by having them all write to the same file, but i would like to be able to have them all create their own file for storing their sequence moves.
How can I make it so a new file is opened every time a new instance of a "rover" is created? If you call a constructor it will open the file and write to it, but if you try to create another rover and call the constructor again it will try to open the same file, and write over anything in it.
rover::rover()
//A rover that defaults to coords (0,0) headed North
{
fout.open("C:/Users/Josh/Desktop/c++/122/sequences.dat");
x = 0;
y = 0;
first_x = 0;
first_y =0;
heading = 'N';
quantity = 0;
fout << "Rover intialized at location (" << x << ", " << y << ", " << heading << ")\n";
}
rover::rover(double initial_x, double initial_y, char initial_heading)
//A rover specifed by the user
{
fout.open("C:/Users/Josh/Desktop/c++/122/sequences.dat");
if((initial_heading == 'N') || (initial_heading == 'n') ||
(initial_heading == 'S') || (initial_heading == 's') ||
(initial_heading == 'E') || (initial_heading == 'e') ||
(initial_heading == 'W') || (initial_heading == 'w'))
{
x = initial_x;
y = initial_y;
first_x = initial_x;
first_y = initial_y;
heading = initial_heading;
quantity = 0;
fout << "Rover intialized at location (" << x << ", " << y << ", " << heading << ")\n";
}
else
cout << "ERROR: Attempted initial heading is not a cardinal direction.\n";
}
In essence, I need to be able to create a new output file for every instance of a constructor when it is called, and then have the class know which file to write to when that particular instance does something...
- Make 'fout' a member of your rover class. That way each file will have it's own 'fout'.
- Have each ctor open a file with a unique name. You can do this by making a static member of rover that counts the number of rovers, and each rover will output to a file something like "sequences_rover_xxx.dat" where xxx is the current count.
Off the top of my head: You could have a member variable for an "id" or something for each rover, and a static variable that indicates what the next id to use is, and use that in the filename. Something like so:
1 2 3 4 5 6 7 8 9 10 11 12 13
class MyClass {
staticunsignedint nextID = 0;
unsignedint ID;
// ...
};
// Then in the constructor
MyClass::MyClass(/* ... */) {
this->ID = nextID++;
std::stringstream ss;
ss<<"C:/Users/Josh/Desktop/c++/122/sequences-rover-"<<this->ID<<".dat";
fout.open(ss.str().c_str());
}
Didn't compile any of this but I think it will work unless I overlooked something. Might not be the best solution either, but it's the first thing I thought of.
thanks that should be really helpful. After the member function counts the number of times it was called is there a way I can make it automatically open a file using the number to create the unique file name without having to do it manually?
For instance can I wrote some sort of code that uses:
Zhuge do you think there's a way to do that without the stringstream and using cout instead? We havent covered that yet in my course so I'm not sure if the prof would allow it.
Any professor that punishes you for independently studying is a moron. The whole point is for you to learn the material. If you're learning on your own, that does nothing but show you're serious and motivated and a good student.
It's fine. He won't have a problem with it.
And no, you can't do it with cout, since cout prints to the console. There are alternative ways, but you likely didn't learn any of them either.