无法从数组中打印信息,无法弄清楚如何遍历存储在类中的数组?
Posted
技术标签:
【中文标题】无法从数组中打印信息,无法弄清楚如何遍历存储在类中的数组?【英文标题】:Having trouble printing the information from an array, can't figure out how to run through the array that was stored in a class? 【发布时间】: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 来保存您的gameobject
s,然后您可以使用size()
函数来获取向量中有多少。
使用std::vector
存储您的gameobject
s,然后您将能够使用.push_back()
添加新的gameobject
实例和.size()
以查看您有多少添加。然后,您需要确保要添加这些的gamelist
在您希望使用它的整个过程中都存在。你现在的做法是创建一个,添加一些东西,然后删除它,这样它就消失了......然后你创建另一个添加到它或从中打印,然后删除那个......等等. 如果您在一组括号中声明它,即使在if
语句正文中,当您离开该部分时它也会被删除
how do I count the number of games added?
这不就是gamecounter
的用途吗?只有你在 add_game
中搞砸了 - 你到底为什么要在那里运行一个循环?
@igor tandetnik 我必须将信息存储在一个数组中,所以我运行了一个 for 循环,将信息存储在一个索引中
我怎么搞砸了我不确定我明白你的意思?
【参考方案1】:
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
【讨论】:
【参考方案2】:1) 您需要声明gamelist
对象不是在本地(每次都在if (option == ...)
范围内),而是在全局范围内:例如,在main
的标头之后:
int main()
//declare you gamelist object here
gamelist run;
...
这样gamelist
对象将在用户输入之间保留并存储信息。
2) addGame()
应该只添加一个游戏并且不需要循环线:for (; gamecounter < 10; gamecounter++)
增加计数器gamecounter++
应该在addGame()
函数结束时完成。
【讨论】:
那么如何在不使用 for 循环 @john_west 的情况下存储在数组的索引中 您将索引存储在gamecounter
变量中。您一次添加一个元素 - 编号为 gamecounter
(取值从 0 到 9),另请参见 Richard Hodges
答案。此外,如果在addGame()
末尾增加计数器,则条件为if (gamecounter > 9)
【参考方案3】:
使用向量来保存游戏对象,或者您可以使用静态成员变量来保存您创建的游戏对象的数量。
对于矢量: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm
对于静态成员: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
【讨论】:
以上是关于无法从数组中打印信息,无法弄清楚如何遍历存储在类中的数组?的主要内容,如果未能解决你的问题,请参考以下文章