【发布时间】:2015-08-07 14:09:31
【问题描述】:
主函数和类定义。 #包括 #包括
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
}
}
}
set 和 get 函数。
//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 函数。我试了一下,但也有一些错误。
提前致谢!!
【问题讨论】:
-
首先使用std::vector 来保存您的
gameobjects,然后您可以使用size()函数来获取向量中有多少。 -
使用
std::vector存储您的gameobjects,然后您将能够使用.push_back()添加新的gameobject实例和.size()以查看您有多少添加。然后,您需要确保要添加这些的gamelist在您希望使用它的整个过程中都存在。你现在的做法是创建一个,添加一些东西,然后删除它,这样它就消失了......然后你创建另一个添加到它或从中打印,然后删除那个......等等. 如果您在一组括号中声明它,即使在if语句正文中,当您离开该部分时它也会被删除 -
how do I count the number of games added?这不就是gamecounter的用途吗?只有你在add_game中搞砸了 - 你到底为什么要在那里运行一个循环? -
@igor tandetnik 我必须将信息存储在一个数组中,所以我运行了一个 for 循环,将信息存储在一个索引中
-
我怎么搞砸了我不确定我明白你的意思?
标签: c++ arrays class for-loop printing