我对 OOP 编程很陌生,想知道如何让我的代码更干净。该程序有效,但我不知道如何为流派、玩家输入等制作不同的类。
我尝试使用 .h 和 .cpp 文件为 Genres 创建一个类,但我仍然没有太多关于如何使我的代码更干净的知识,因为所有内容都是在:int main () {}
bookPicker.cpp
int main()
{
//Declaring a user account
std::string name, lastName;
//Genre's
std::string ice, fire, earth, wind;
ice = "Ice", fire = "Fire", earth = "Earth", wind = "Wind";
//Titles
int a, b, c, d;
//Recommendation
std::string r;
r = "Type yess if this is the genre of your choice\n";
std::string iceS[4] = { "The Ice Gauntlet", "The Formal Ice Queen", "Frozen in Time", "Frost Lake" };
std::string fireS[4] = { "The Fire Gauntlet", "The Formal Fire Queen", "Hot Air", "Fire Lake" };
std::string earthS[4] = { "The Earth Gauntlet", "The Formal Earth Queen", "Stuck in Time", "The Swamp" };
std::string windS[4] = { "The Wind Gauntlet", "The Formal Wind Queen", "Blown in Time", "Wind Lake" };
//Welcome
std::string w, wU;
w = "Welcome ";
wU = " to The Four Elemets Book Store!\n";
//Creating user account
std::cout << "Please enter your name" << std::endl;
std::cin >> name;
std::cout << "Please enter your lastname" << std::endl;
std::cin >> lastName;
std::string userAccount = name + lastName;
//Ask for input
std::cout << w << userAccount << std::endl;
std::cout << "What kind of genre do you like to read? \n" << std::endl;
std::cout << ice << "\n" << fire << "\n" << earth << "\n" << wind << "\n" << std::endl;
std::cout << "Please pick your genre\n" << std::endl;
//create the choice string variable
std::string choice;
std::cin >> choice;
//if statement after the input
if (choice == ice) {
std::cout << r << std::endl;
std::cin >> a;
std::cout << "\n";
for (int i = 0; i < 4; i++) {
std::cout << iceS[i] << "\n";
}
} if (choice == fire) {
std::cout << r << std::endl;
std::cin >> b;
std::cout << "\n";
for (int y = 0; y < 4; y++) {
std::cout << fireS[y] << "\n";
}
} if (choice == earth) {
std::cout << r << std::endl;
std::cin >> c;
std::cout << "\n";
for (int x = 0; x < 4; x++) {
std::cout << earthS[x] << "\n";
}
} if (choice == wind) {
std::cout << r << std::endl;
std::cin >> d;
std::cout << "\n";
for (int o = 0; o < 4; o++) {
std::cout << windS[o] << "\n";
}
}
return 0;
}