First of all, hello!Im new to the forums. Im still kind of new to c++, and im working on understanding classes at the moment. Below is some code ive typed up that im having trouble with. Here is the code.
-- Sorry if its a lot ---Code:#include <iostream> std::string cClass; class Player{ public: struct sPLAYER{ int hp; int en; int mana; int gold; }; sPLAYER sP; void setName(std::string cName1){ cName2 = cName1; } std::string getName(){ return cName2; } void setClass(int n){ if( n == 1 ){ cClass = "Knight"; } else if( n == 2 ){ cClass = "Mage"; } else if( n == 3 ){ cClass = "Thief"; } else{ std::cout << "\n\n - Invalid Class Type - "; } } std::string getClass(){ return cClass; } void setStats(std::string cClass){ if( cClass == "Knight" ){ sP.hp = 20; sP.en = 20; sP.mana = 13; sP.gold = 100; } else if( cClass == "Mage" ){ sP.hp = 25; sP.en = 14; sP.mana = 25; sP.gold = 100; } else if( cClass == "Thief" ){ sP.hp = 17; sP.en = 23; sP.mana = 13; sP.gold = 115; } else{ std::cout << " Error - Invalid Class "; } } int getStats(){ std::cout << " HP: " << sP.hp << std::endl; std::cout << " Energy: " << sP.en << std::endl; std::cout << " Mana: " << sP.mana << std::endl; std::cout << " Starting Gold: " << sP.gold << std::endl; } private: std::string cName2; }; Player Player; int main(){ int n; std::string cName1; std::cout << "\n\n Enter Character Name: "; std::cin >> name1; Player.setName(name1); std::cout << "\n\n --- Select Character Class ---"; std::cout << "\n\n 1. Knight"; std::cout << "\n 2. Mage"; std::cout << "\n 3. Theif\n "; std::cin >> n; Player.setClass(n); Player.setStats(cClass); std::cin.get(); std::cout << "\n\n Player Information "; std::cout << "\n\n Name: " << Player.getName() << std::endl; std::cout << " Class: " << Player.getClass() << std::endl; std::cout << " Stats: " << Player.getStats() << std::endl; std::cin.get(); }
Ok, so here's the problem. When I run the program, I enter a character name, and then I pick a class type. After I pick the class type it shows my characters name, class type and stats. But the problem is how it shows the information. Here is a demonstration.
I cant figure out why it displays the class stats, then displays 'Stats:' with some random number. Can anyone enlighten me on the problem and how to fix it?Code:Enter Character Name: Luminost --- Select Character Class --- 1. Knight 2. Mage 3. Theif 1 Player Information Name: Luminost Class: Knight HP: 20 Energy: 20 Mana: 13 Starting Gold: 100 Stats: 4473824



LinkBack URL
About LinkBacks
Im new to the forums. Im still kind of new to c++, and im working on understanding classes at the moment. Below is some code ive typed up that im having trouble with. Here is the code.


