主要功能和类定义。 #包括 #include
using namespace std;
class gameobject
{
public:
void set_id_num(int num);
int get_id_num();
void set_name(string name1);
string get_name();
void set_type(int type_of_game);
string get_type();
void set_buy_value(float buy_game);
float get_buy_value();
void set_market_value(float market_price);
float get_market_value();
void set_year(int year1);
int get_year();
private:
int id_num;//identifier number for the game
string name;//the name of the game
int type;//whether the game is cartridge, CD, DVD, BR, download
string type_name;//type of game
float buy_value;//price of game
float market_value;//value of game
int year;//year the game was made
};
class gamelist
{
private:
int gamecounter = 0;
gameobject gameobjects[10];
public:
void add_game();
void print_list();
float total_value();
};
int main()
{
int option;//menu choice
do
{
//menu
cout << endl;
cout << "Please choose an option from the below menu. " << endl;
cout << "1. Add Game" << endl;
cout << "2. Print List" << endl;
cout << "3. Total value of collection" << endl;
cout << "4. Delete Game" << endl;
cout << "5. Exit" << endl;
cout << "Which would you like to execute? ";
cin >> option;
cin.ignore();
//to add the games
if (option == 1)
{
gamelist run;
run.add_game();//goes through the options for setting up a game
}
//print game info
else if (option == 2)
{
gamelist run;
run.print_list();
}
//total value
else if (option == 3)
{
gamelist run;
run.total_value();
}
} while (option != 5);
if (option == 5)
return 0;
}
我遇到问题的地区。
//adds the inputted market value in the array
float gamelist::total_value()
{
float value_of_games = 0;
cout << "The value of the games is: ";
for (int i = 0; i < gamecounter; i++)
{
value_of_games = gameobjects[i].get_market_value() + value_of_games;
}
return(value_of_games);
}
//prints the info in the array
void gamelist::print_list()
{
cout << "The game information is listed below: " << endl;
for (int i = 0; i < gamecounter; i++)
{
cout << gameobjects[i].get_id_num() << endl;
cout << gameobjects[i].get_name() << endl;
cout << gameobjects[i].get_type() << endl;
cout << gameobjects[i].get_buy_value() << endl;
cout << gameobjects[i].get_market_value() << endl;
cout << gameobjects[i].get_year() << endl;
}
}
//to add a game to the lise
void gamelist::add_game()
{
gamecounter++;
if (gamecounter > 10)
{
cout << "You cannot add any more games. ";
}
else
{
int id;
string name_game;
int type_game;
int buy;
int market;
int year_game;
cout << "Please enter an id number for the game: ";
cin >> id;
cout << "Please enter a name for the game: ";
cin.ignore();
getline(cin, name_game);
cout << "There are four types of games." << endl;
cout << " 0. Cartridge " << endl;
cout << " 1. CD " << endl;
cout << " 2. DVD " << endl;
cout << " 3. BR " << endl;
cout << " 4. Download " << endl;
cout << "Which type do you want to set for the game (enter number)? ";
cin >> type_game;
cout << "Please set a buying value for the game: ";
cin >> buy;
cout << "Please set the market value of the game: ";
cin >> market;
cout << "What is the model year of the game? ";
cin >> year_game;
for (; gamecounter < 10; gamecounter++)
{
gameobjects[gamecounter].set_id_num(id);//passes value
gameobjects[gamecounter].set_id_num(id);//passes value
gameobjects[gamecounter].set_name(name_game);//passes value
gameobjects[gamecounter].set_type(type_game);//passes value
gameobjects[gamecounter].set_buy_value(buy);//passes value
gameobjects[gamecounter].set_market_value(market);//passes value
gameobjects[gamecounter].set_year(year_game);//passes value
}
}
}
设置和获取功能。
//sets id num for the game
void gameobject::set_id_num(int num)
{
id_num = num;
}
//displays the id num for the game
int gameobject::get_id_num()
{
return(id_num);
}
//sets desired name for game
void gameobject::set_name(string name1)
{
name = name1;
}
//displays the name of the game
string gameobject::get_name()
{
return(name);
}
//presents a menu to choose type of game
void gameobject::set_type(int type_of_game)
{
type = type_of_game;
}
//prints the type of game chosen
string gameobject::get_type()
{
if (type == 0)
{
type_name = "cartridge";
return(type_name);
}
else if (type == 1)
{
type_name = "CD";
return(type_name);
}
else if (type == 2)
{
type_name = "DVD";
return(type_name);
}
else if (type == 3)
{
type_name = "BR";
return(type_name);
}
else if (type == 4)
{
type_name = "download";
return(type_name);
}
}
//sets the buying value of game
void gameobject::set_buy_value(float buy_game)
{
buy_value = buy_game;
}
//displays the buying value for game
float gameobject::get_buy_value()
{
return(buy_value);
}
//sets market value
void gameobject::set_market_value(float market_price)
{
market_value = market_price;
}
//displays market value
float gameobject::get_market_value()
{
return(market_value);
}
//sets model year of the game
void gameobject::set_year(int year1)
{
year = year1;
}
//displays model year
int gameobject::get_year()
{
return(year);
}
我的问题是,我如何计算添加的游戏数量?因为我这样做的方式不起作用。
如何更改打印功能以便实际打印某些内容?我的代码不是写在那里,但经过一些研究,我真的不知道为什么它不起作用。
最后,我完全不知道从每场比赛中添加市场价值来获得总价值。这是total_value函数。我试一试,但也有一些错误。
提前致谢!!
答案 0 :(得分:1)
c ++数组从0开始编入索引。当你添加第一个游戏时,它需要在索引0处,这意味着你必须在之后增加gamecounter
你已经将游戏细节输入到游戏阵列。
gamecounter
可以被认为是两件事:
现在阵列中的游戏数量
next 游戏的索引(不是数组中最后一个游戏的索引)
如果您按照这种方式工作,那么gamecounter
将正确表示游戏数量。
当然,我不会第一个告诉你使用std :: vector来存储你的游戏。然后你可以这样做:
添加游戏:
gameobject go(...parameters...);
go.set...();
gameobjects.push_back(std::move(go));
获得游戏数量:
return gameobjects.size();
迭代所有游戏:
for(const auto& game : gameobjects) {
// do what you need to do on game
}
答案 1 :(得分:0)
1)您需要在本地(每次gamelist
范围内)声明if (option == ...)
对象,但需要全局声明:例如,在main
的标题之后:
int main() {
//declare you gamelist object here
gamelist run;
...
}
这样gamelist
对象将保留用户输入并存储信息。
2)addGame()
应该只添加一个游戏并且不需要循环线:for (; gamecounter < 10; gamecounter++)
递增计数器gamecounter++
应在addGame()
函数结束时完成。
答案 2 :(得分:0)
使用向量来保存游戏对象,或者您可以使用静态成员变量来保存您创建的游戏对象的数量。
对于静态成员: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/