LukeA
06-29-2004, 03:43 AM
Thanks for the reply.. I had everything running smoothly up until this point, a little help is all I need.. I don't expect, or want someone to do it for me, I want to figure out where my error for this and future reference. My .cpp file is fairly large, so I edited out functions and other parts that do not pertain to this problem:
//*********** Prototypes **************************
void fillBaseball(Baseball baseball[]);
void fillDriving(Driving driving[]);
void fillFPS(FPS shooter[]);
void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int FPS_SIZE);
void createSelection(Baseball baseball[], int& base_size);
void createBaseball(Baseball baseball[], int& size);
void createDriving();
void createFPS();
void invalidSelection();
//*********** main *******************************
int main()
{
bool okay, quit(0);
char selection;
int base_size = 6;
int driv_size = 1;
int shooter_size = 1;
Baseball* baseball;
baseball = new Baseball[base_size];
fillBaseball(baseball); // fills baseball array of Baseball objects
Driving* driving;
driving = new Driving[driv_size];
fillDriving(driving); // fills driving array of Baseball objects
FPS* shooter;
shooter = new FPS[shooter_size];
fillFPS(shooter); // fills FPS array of Baseball objects
do
{
do
{
cout<< "\n\t1 - Display Objects"
<< "\n\t2 - Sort Objects"
<< "\n\t3 - Create Object"
<< "\n\t4 - Exit Program"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '4');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);
switch (selection)
{
case '1':
outputSelection(baseball, base_size, driving, driv_size, shooter, shooter_size);
break;
case '2':
//baseball[base_size-1].output();
break;
case '3':
createSelection(baseball, base_size);
break;
case '4':
quit = 1;
break;
}
} while (! quit);
cout<< endl << endl << "Goodbye!\n\n";
system("Pause");
return 0;
}
void createSelection(Baseball baseball[], int& base_size)
{
char selection;
bool okay;
do
{
cout<< "\n\t1 - Add a Baseball Game"
<< "\n\t2 - Add a Driving Game"
<< "\n\t3 - Add a First Person Shooter Game"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);
switch (selection)
{
case '1':
createBaseball(baseball, base_size);
break;
case '2':
createDriving();
break;
case '3':
createFPS();
break;
}
}
void createBaseball(Baseball baseball[], int& size)
{
string name, console, genre, type;
char char_online, char_type, char_roster, char_minors, char_derby;
bool okay, online, roster, minors, derby;
int players;
double price;
cout<< "Enter the Name of the game: ";
getline(cin,name);
cout<< "Enter the Console the game is for: ";
getline(cin,console);
cout<< "Enter the Genre of the game: ";
getline(cin,genre);
Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];
delete [] baseball;
size++;
baseball = new Baseball[size];
for (int j=0; j < size-1; j++)
baseball[j] = temp[j];
delete [] temp;
baseball[size-1].setValues(name, console, genre, online, players, price, type,
roster, minors, derby);
for (int k=0; k < size; k++)
baseball[k].output();
}
And as stated in my original post, I can output the data in the object- as the last 2 lines of my code show- fine, but when I get back into main, attempting to output the data results in an error message when trying to access the first variable. Thanks again for your help.
LukeA
06-29-2004, 04:34 AM
Ok, sorry I didn't think about putting it into a compiler.. here is the complete .cpp with the class definitions.
/* Luke G Adams
June 23, 2004
COSC 201
Project 5
*/
//*********** Includes *********************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <cassert>
using namespace std;
//*********** Game BASE Class ***************************************
class Game
{
public:
Game(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value);
Game() { };
protected:
string name;
string console;
string genre;
bool online;
int players;
double price;
};
Game::Game(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
}
//*********** Sports Class *****************************************
class Sports:public Game
{
public:
Sports(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value);
Sports() { };
void output();
protected:
string type;
bool rosterUpdates;
};
Sports::Sports(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
type = type_value;
rosterUpdates = roster_value;
}
//*********** Simulation Class *************************************
class Simulation:public Game
{
public:
Simulation(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value);
Simulation() { };
void output();
protected:
bool downContent;
};
Simulation::Simulation(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
downContent = content_value;
}
//*********** Action Class *****************************************
class Action:public Game
{
public:
Action(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value);
Action() { };
void output();
protected:
bool downContent;
bool coop;
};
Action::Action(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value)
: Game(name_value, console_value, genre_value,
online_value, players_value, price_value)
{
downContent = content_value;
coop = coop_value;
}
//*********** Baseball Class ***************************************
class Baseball:public Sports
{
public:
//Baseball(string name_value, string console_value, string genre_value,
// bool online_value, int players_value, double price_value,
// string type_value, bool roster_value, bool minors_value,
// bool derby_value);
Baseball() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value);
void output();
private:
bool minors;
bool derby;
};
/*
Baseball::Baseball(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value)
: Sports(name_value, console_value, genre_value, online_value,
players_value, price_value, type_value, roster_value)
{
minors = minors_value;
derby = derby_value;
}
*/
void Baseball::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
string type_value, bool roster_value, bool minors_value,
bool derby_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
type = type_value;
rosterUpdates = roster_value;
minors = minors_value;
derby = derby_value;
}
void Baseball::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;
cout<< setw(18) << "Console: ";
cout<< console << endl;
cout<< setw(18) << "Genre: ";
cout<< genre << endl;
cout<< setw(18) << "Online: ";
cout<< online << endl;
cout<< setw(18) << "Roster Updates: ";
cout<< rosterUpdates << endl;
cout<< setw(18) << "Players: ";
cout<< players << endl;
cout<< setw(18) << "Type: ";
cout << type << endl;
cout<< setw(18) << "Minor Leagues: ";
cout<< minors << endl;
cout<< setw(18) << "Homerun Derby: ";
cout<< derby << endl;
cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}
//*********** Driving Class ****************************************
class Driving:public Simulation
{
public:
Driving() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool split_value, bool drag_value);
void output();
protected:
bool splitScreen;
bool dragRacing;
};
void Driving::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool split_value, bool drag_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
downContent = content_value;
splitScreen = split_value;
dragRacing = drag_value;
}
void Driving::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;
cout<< setw(18) << "Console: ";
cout<< console << endl;
cout<< setw(18) << "Genre: ";
cout<< genre << endl;
cout<< setw(18) << "Online: ";
cout<< online << endl;
cout<< setw(18) << "Content Download: ";
cout<< downContent << endl;
cout<< setw(18) << "Players: ";
cout<< players << endl;
cout<< setw(18) << "Split Screen: ";
cout << splitScreen << endl;
cout<< setw(18) << "Drag Racing: ";
cout<< dragRacing << endl;
cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}
//*********** FPS Class ********************************************
class FPS:public Action
{
public:
FPS() { /*Default Constructor*/ };
void setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value, bool vehicle_value,
int levels_value);
void output();
protected:
bool vehicles;
int levels;
};
void FPS::setValues(string name_value, string console_value, string genre_value,
bool online_value, int players_value, double price_value,
bool content_value, bool coop_value, bool vehicle_value,
int levels_value)
{
name = name_value;
console = console_value;
genre = genre_value;
online = online_value;
players = players_value;
price = price_value;
downContent = content_value;
coop = coop_value;
vehicles = vehicle_value;
levels = levels_value;
}
void FPS::output()
{
cout<< setw(18) << "Name: ";
cout<< name << endl;
cout<< setw(18) << "Console: ";
cout<< console << endl;
cout<< setw(18) << "Genre: ";
cout<< genre << endl;
cout<< setw(18) << "Online: ";
cout<< online << endl;
cout<< setw(18) << "Content Download: ";
cout<< downContent << endl;
cout<< setw(18) << "Players: ";
cout<< players << endl;
cout<< setw(18) << "2 Player Co-Op: ";
cout << coop << endl;
cout<< setw(18) << "Vehicles: ";
cout<< vehicles << endl;
cout<< setw(18) << "# of Levels: ";
cout<< levels << endl;
cout<< setw(19) << "Price: $";
cout<< price << endl << endl;
}
//*********** Output Objects ***************************************
template <class Item>
void output(Item data[], const int SIZE)
{
for (int i=0; i < SIZE; i++)
data[i].output();
}
//*********** Prototypes *******************************************
void fillBaseball(Baseball baseball[]);
void fillDriving(Driving driving[]);
void fillFPS(FPS shooter[]);
void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int FPS_SIZE);
void createSelection(Baseball baseball[], int& base_size);
void createBaseball(Baseball baseball[], int& size);
void createDriving();
void createFPS();
void invalidSelection();
//*********** main *************************************************
int main()
{
bool okay, quit(0);
char selection;
int base_size = 6;
int driv_size = 1;
int shooter_size = 1;
Baseball* baseball;
baseball = new Baseball[base_size];
fillBaseball(baseball); // fills baseball array of Baseball objects
Driving* driving;
driving = new Driving[driv_size];
fillDriving(driving); // fills driving array of Baseball objects
FPS* shooter;
shooter = new FPS[shooter_size];
fillFPS(shooter); // fills FPS array of Baseball objects
do
{
do
{
cout<< "\n\t1 - Display Objects"
<< "\n\t2 - Sort Objects"
<< "\n\t3 - Create Object"
<< "\n\t4 - Exit Program"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '4');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);
switch (selection)
{
case '1':
outputSelection(baseball, base_size, driving, driv_size, shooter, shooter_size);
break;
case '2':
//baseball[base_size-1].output();
break;
case '3':
createSelection(baseball, base_size);
break;
case '4':
quit = 1;
break;
}
} while (! quit);
cout<< endl << endl << "Goodbye!\n\n";
system("Pause");
return 0;
}
void outputSelection(Baseball baseball[], const int BASE_SIZE,
Driving driving[], const int DRIV_SIZE,
FPS shooter[], const int SHOOTER_SIZE)
{
char selection;
bool okay;
do
{
cout<< "\n\t1 - Output Baseball Games"
<< "\n\t2 - Output Driving Games"
<< "\n\t3 - Output First Person Shooter Games"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);
switch (selection)
{
case '1':
output(baseball, BASE_SIZE);
break;
case '2':
output(driving, DRIV_SIZE);
break;
case '3':
output(shooter, SHOOTER_SIZE);
break;
}
}
void createSelection(Baseball baseball[], int& base_size)
{
char selection;
bool okay;
do
{
cout<< "\n\t1 - Add a Baseball Game"
<< "\n\t2 - Add a Driving Game"
<< "\n\t3 - Add a First Person Shooter Game"
<< "\n\n\tPlease make your selection: ";
cin>> selection;
cin.ignore(100, '\n');
selection = toupper(selection);
okay = (selection >= '1' && selection <= '3');
if (! okay)
{
invalidSelection();
system("Pause");
}
}
while (! okay);
switch (selection)
{
case '1':
createBaseball(baseball, base_size);
break;
case '2':
createDriving();
break;
case '3':
createFPS();
break;
}
}
void createBaseball(Baseball baseball[], int& size)
{
string name, console, genre, type;
char char_online, char_type, char_roster, char_minors, char_derby;
bool okay, online, roster, minors, derby;
int players;
double price;
cout<< "Enter the Name of the game: ";
getline(cin,name);
cout<< "Enter the Console the game is for: ";
getline(cin,console);
cout<< "Enter the Genre of the game: ";
getline(cin,genre);
do
{
cout<< "Does this game offer online play? (y/n) ";
cin>> char_online;
cin.ignore(100, '\n');
char_online = toupper(char_online);
okay = (char_online == 'Y' || char_online == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_online == 'Y')
online = 1;
else
online = 0;
} while (! okay);
do
{
cout<< "What is the maximum number of players this game offers? (1-4) ";
cin>> players;
cin.ignore(100, '\n');
okay = (players >= 1 && players <= 4);
if (! okay)
{
invalidSelection();
system("Pause");
}
} while (! okay);
do
{
cout<< "Enter the price of this game: $";
cin>> price;
cin.ignore(100, '\n');
} while (! okay);
do
{
cout<< "Is the game type: 1 - Arcade Style or 2 - Simulation Style? ";
cin>> char_type;
cin.ignore(100, '\n');
okay = (char_type == '1' || char_type == '2');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_online == '1')
type = "Arcade";
else
type = "Simulation";
} while (! okay);
do
{
cout<< "Does this game offer online roster updates? (y/n) ";
if (! online)
cout<< "N/A\n";
else
{
cin>> char_roster;
cin.ignore(100, '\n');
char_roster = toupper(char_roster);
okay = (char_roster == 'Y' || char_roster == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_roster == 'Y')
roster = 1;
else
roster = 0;
}
} while (! okay);
do
{
cout<< "Does this have a Minor League system/rosters? (y/n) ";
cin>> char_minors;
cin.ignore(100, '\n');
char_minors = toupper(char_minors);
okay = (char_minors == 'Y' || char_minors == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_minors == 'Y')
minors = 1;
else
minors = 0;
} while (! okay);
do
{
cout<< "Does this game offer a Home Run Derby? (y/n) ";
cin>> char_derby;
cin.ignore(100, '\n');
char_derby = toupper(char_derby);
okay = (char_derby == 'Y' || char_derby == 'N');
if (! okay)
{
invalidSelection();
system("Pause");
}
if (char_derby == 'Y')
derby = 1;
else
derby = 0;
} while (! okay);
Baseball* temp = new Baseball[size];
for (int i=0; i < size; i++)
temp[i] = baseball[i];
delete [] baseball;
size++;
baseball = new Baseball[size];
for (int j=0; j < size-1; j++)
baseball[j] = temp[j];
delete [] temp;
baseball[size-1].setValues(name, console, genre, online, players, price, type,
roster, minors, derby);
for (int k=0; k < size; k++)
baseball[k].output();
}
void createDriving()
{
}
void createFPS()
{
}
void fillBaseball(Baseball baseball[])
{
baseball[0].setValues("ESPN Major League Baseball", "X-Box", "Sports", 1, 4, 39.95,
"Simulation", 1, 1, 0);
baseball[1].setValues("MLB SlugFest: Loaded", "X-Box", "Sports", 1, 4, 39.95,
"Arcade", 1, 0, 1);
baseball[2].setValues("MLB 2005", "Playstation 2", "Sports", 1, 2, 39.95,
"Simulation", 1, 0, 1);
baseball[3].setValues("MVP Baseball 2004", "X-Box", "Sports", 1, 2, 49.95,
"Simulation", 1, 1, 1);
baseball[4].setValues("All-Star Baseball 2005", "X-Box", "Sports", 1, 4, 39.95,
"Simulation", 1, 0, 1);
baseball[5].setValues("High Heat Major League Baseball", "Playstation 2", "Sports", 0, 2, 29.95,
"Simulation", 0, 0, 1);
}
void fillDriving(Driving driving[])
{
driving[0].setValues("Need For Speed Underground", "X-Box", "Simulation", 0, 2, 49.95,
0, 1, 1);
}
void fillFPS(FPS shooter[])
{
shooter[0].setValues("Halo", "X-Box", "Action", 0, 2, 29.95,
0, 1, 1, 14);
}
//*********** invalidSelection **************************************
void invalidSelection()
{
cout<< "\nERROR: You have made an invalid selection. Please try again.\n";
}
I'm not sure why all my functions are void, I guess I didn't see another way of doing it.. Also, this is my first time working with inheritance and derived classes, so sorry if it looks kinda messy..
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.