Hi everyone
I have written a simple database creator program but get an exception when I switch to release mode in vc++ 2008 express and run it (it's fine in debug mode).
Here is my code (unsure how to put it in a block):
Excerpt from main.cpp:
int main()
{
vector<Player*> database;
bool quit = false;
// Welcome
cout << "Welcome to Fantasy Football Database Creator!" << endl;
cout << "You can start a new database or load an existing one for editing" << endl << endl;
while( !quit )
{
int choice = 0;
//////////////
// MAIN MENU//
//////////////
choice = MenuSelection("Database- 1)Add, 2)Delete, 3)Save, 4)Load, 5)View, 6)Clear, 7)Exit: ", 1, 7);
// Call function depending on user's main menu selection
switch(choice)
{
case 1:
AddPlayer(database);
break;
case 2:
RemovePlayer(database);
break;
case 3:
SaveDatabase(database);
break;
case 4:
LoadDatabase(database);
break;
case 5:
ViewDatabase(database);
break;
case 6:
ClearDatabase(database);
break;
default:
ClearDatabase(database);
quit = true;
break;
}
}
}
// Save database with error checking
void SaveDatabase(vector<Player*>& database)
{
if(database.empty())
{
cout << "Database is empty!" << endl << endl;
return;
}
string saveFileName = "";
cout << "File name to save to: ";
cin >> saveFileName;
ofstream outFile(saveFileName.c_str(), ios_base::binary);
if(outFile)
{
cout << "Size of database is " << database.size() << endl;
cout << "Saving..." << endl << endl;
for(unsigned int i = 0; i < database.size(); ++i)
{
outFile.write((char*)&(*database), sizeof(Player));
}
}
else
{
cout << "Save failed!" << endl;
}
}
// Load database with error checking
void LoadDatabase(vector<Player*>& database)
{
string loadFileName = "";
cout << "File name to load from: ";
cin >> loadFileName;
ifstream inFile(loadFileName.c_str(), ios_base::binary);
Player player;
Player* playerPointer;
if(inFile)
{
while(!inFile.eof())
{
inFile.read((char*)&player, sizeof(Player));
playerPointer = new Player(player);
database.push_back(playerPointer);
}
int size = database.size();
delete database[size - 1];
database[size - 1] = 0;
database.pop_back();
cout << "Load succeeded!" << endl;
}
else
{
cout << "Load failed!" << endl;
}
}
Player.h/cpp
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
class Player
{
public:
Player();
Player(std::string name, std::string position, int value);
~Player();
void View();
std::string mName;
std::string mPosition;
int mValue;
int mWeekPoints;
int mOverallPoints;
};
#endif // PLAYER_H
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player()
: mName("Default"), mPosition("Default"), mValue(0), mWeekPoints(0), mOverallPoints(0)
{}
Player::Player(std::string name, std::string position, int value)
: mName(name), mPosition(position), mValue(value), mWeekPoints(0), mOverallPoints(0)
{}
Player::~Player()
{}
void Player::View()
{
cout << "Name: " << mName << endl;
cout << "Position: " << mPosition << endl;
cout << "Value: " << mValue << endl << endl;
}
When I run the program in release mode, the debugger stops in the LoadDatabase function on the line:
playerPointer = new Player(player);
And the following message displays:
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in Fantasy Football.exe
Additional information: External component has thrown an exception.
When I look around for solutions, they seem to be related to Windows forms stuff that I'm not using and may also be a problem specific to Windows XP but that doesn't help me much :(.
I think it must have to do with my load function or the way I originally save but I don't know. Does anyone know why this problem occurs and if there are any known workarounds please?
Cheers, Chris.