I've searched google and tried a few things myself.. and I've found nothing and I know its a simple fix. Can someone please point out what I'm over looking? ><
void PrintMap(vector<string> mapdata)
{
vector<string> temp = mapdata;/// temp is a copy mapdata{vector}
for (auto i = temp.begin(); i != temp.end(); ++i) ///auto i , 'i' is an iterator
{
//printf("%s\n", temp[i].c_str());
cout << temp << endl; ///vectors have no overloaded << operator because they
/// are generic containers
cout << temp[i] << endl; /// i is an iterator not an integral index.
}
}
void PrintMap(const vector<string>& mapdata)///copying vectors can be costly at times so prefer const reference to passing by value.
{
///our vector is passed as an immutable object hence we dont need a copy here.
for (auto i = mapdata.begin(); i != mapdata.end(); ++i)
{
//printf("%s\n", (*i).c_str()); could have been the right way
///cout << temp << endl; you dont need this
cout << *i << endl; //what you needed.
}
}
Edit: Also there is a logical bug on how you handle error checking on some of your files
void WriteToFile()
{
ofstream file;
file.open("MyFile.txt");
if (file.fail())
{
cout << "ERROR: Could not open file!" << endl;
perror("MyFile.txt");
cout << "ERROR: Could not open file!" << endl;
}///what happens when we get here , i guess execution continues and all operataions below would be invalid, advice: if the check above was true simply exit this
///function or use better error handling mechanism.
file << "Map:test" << endl;
file << "XXX" << endl;
file << "XXX" << endl;
file << "XXX" << endl;
file.close();
cout << "Writing to file was a Succses!" << endl;
}
void LoadMap(vector<string> &mapData, string fileName)
{
ifstream file(fileName);///prefer constructors to the open function, that way you wouldn't need to close the stream
if (!file)///similarly use streams direcly to check for a wider range of errors
{
cout << "===========================" << endl;
cout << "ERROR: Could not open file!\n" << endl;
perror(fileName.c_str());
cout << "===========================" << endl;
///system("PAUSE"); avoid system calls as much as possible they are very slow
return;//better
}
string lineContent;
while (getline(file, lineContent))
{
mapData.push_back(lineContent);
}
///file.close(); not needed
cout << "Loading Map was a Succses!\n" << endl;
}