从数组中打印信息时遇到问题,无法弄清楚如何 运行 通过存储在 class 中的数组?
Having trouble printing the information from an array, can't figure out how to run through the array that was stored in a class?
主要函数和 class 定义。
#包括
#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
}
}
}
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 函数。我试了一下,但那里也有一些错误。
提前致谢!!
1) 您需要声明 gamelist
object 不是本地(每次都在 if (option == ...)
范围内)而是全局声明:例如,在 [=13 的 header 之后=]:
int main() {
//declare you gamelist object here
gamelist run;
...
}
这样 gamelist
object 将在用户输入之间保留并存储信息。
2) addGame()
应该只添加一个游戏并且不需要循环线:for (; gamecounter < 10; gamecounter++)
增加计数器 gamecounter++
应该在 addGame()
函数结束时完成。
c++ 数组从 0 开始索引。当您添加第一个游戏时,它需要从索引 0 开始,这意味着您必须在输入 gamecounter
之后递增 游戏详情进入游戏数组。
gamecounter
可以认为是两件事:
现在数组中的游戏数
下一场比赛的索引(不是数组中最后一场比赛的索引)
如果您这样计算,那么 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
}
使用vector来保存游戏对象或者你可以使用静态成员变量来保存你创建的游戏对象的数量。
对于静态成员:
http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
主要函数和 class 定义。 #包括 #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
}
}
}
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 函数。我试了一下,但那里也有一些错误。
提前致谢!!
1) 您需要声明 gamelist
object 不是本地(每次都在 if (option == ...)
范围内)而是全局声明:例如,在 [=13 的 header 之后=]:
int main() {
//declare you gamelist object here
gamelist run;
...
}
这样 gamelist
object 将在用户输入之间保留并存储信息。
2) addGame()
应该只添加一个游戏并且不需要循环线:for (; gamecounter < 10; gamecounter++)
增加计数器 gamecounter++
应该在 addGame()
函数结束时完成。
c++ 数组从 0 开始索引。当您添加第一个游戏时,它需要从索引 0 开始,这意味着您必须在输入 gamecounter
之后递增 游戏详情进入游戏数组。
gamecounter
可以认为是两件事:
现在数组中的游戏数
下一场比赛的索引(不是数组中最后一场比赛的索引)
如果您这样计算,那么 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
}
使用vector来保存游戏对象或者你可以使用静态成员变量来保存你创建的游戏对象的数量。
对于静态成员: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/