[英]Having trouble printing the information from an array, can't figure out how to run through the array that was stored in a class?
主要功能和類定義。 #include #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函數。 我嘗試了一下,但是那里也有一些錯誤。
提前致謝!!
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
}
1)您需要不在本地(每次在if (option == ...)
范圍內)聲明gamelist
對象,而是在全局范圍內聲明:例如,在main
標頭之后:
int main() {
//declare you gamelist object here
gamelist run;
...
}
這樣, gamelist
對象將在用戶輸入之間保留並存儲信息。
2) addGame()
應該只添加一個游戲,並且不需要循環線: for (; gamecounter < 10; gamecounter++)
遞增計數器gamecounter++
應該在addGame()
函數的末尾進行。
使用vector來保存游戲對象,或者可以使用靜態成員變量來保存創建的游戲對象的數量。
對於矢量: 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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.