### Help with Functions

UPDATE
Hello, just started learning C++ and Im writing a little game.

How would I get line 148-156 to rerun after PlayerClass is done? Ive looked at for loops, but cant find anything that would let me look around the room again after I open one of the door, window, or chest.
Also any help on errors that you might find would be great, variables are displaying strange values in the program.

I have the feeling im just writing this completely wrong, any info on how Id do it would be much appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165`` ``````#include #include using namespace std; int PlayerStats() { int spec; int stamina; int agility; int intellect; int luck; cout << "\nSelect Class Stats to View\n" << endl; cout << "1 = Warrior" << endl; cout << "2 = Mage" << endl; cout << "3 = Rogue" << endl; cout << "4 = Priest" << endl; if (spec == 1) {stamina = 4, agility = 2, intellect = 0, luck = 2;} else if (spec == 2) {stamina = 1, agility = 1, intellect = 4, luck = 3;} else if (spec == 3) {stamina = 1, agility = 4, intellect = 1, luck = 4;} else if (spec == 4) {stamina = 1, agility = 0, intellect = 5, luck = 2;} cin >> spec; cout << "Current Class Stats \n\n" << endl; cout << "Stamina = " << stamina << endl; cout << "Intellect = " << intellect << endl; cout << "Agility = " << agility << endl; cout << "Luck = " << luck << endl; return spec; } int PlayerClass() { string playerclass; int stam; int agil; int spell; int luck; cout << " Gift of the Warrior Gift of the Rogue\n" << endl; cout << " Gift of the Mage Gift of the Priest\n\n" << endl; getline(cin, playerclass); if (playerclass == "Gift of the Warrior") {stam = 4, agil = 2, spell = 0, luck = 2;} else if (playerclass == "gift of the warrior") {stam = 4, agil = 2, spell = 0, luck = 2;} else if (playerclass == "warrior") {stam = 4, agil = 2, spell = 0, luck = 2;} else if (playerclass == "Gift of the Rogue") {stam = 1, agil = 4, spell = 1, luck = 4;} else if (playerclass == "gift of the rogue") {stam = 1, agil = 4, spell = 1, luck = 4;} else if (playerclass == "rogue") {stam = 1, agil = 4, spell = 1, luck = 4;} else if (playerclass == "Gift of the Mage") {stam = 1, agil = 1, spell = 4, luck = 3;} else if (playerclass == "gift of the mage") {stam = 1, agil = 1, spell = 4, luck = 3;} else if (playerclass == "mage") {stam = 1, agil = 1, spell = 4, luck = 3;} else if (playerclass == "Gift of the Priest") {stam = 1, agil = 0, spell = 5, luck = 3;} else if (playerclass == "gift of the priest") {stam = 1, agil = 0, spell = 5, luck = 3;} else if (playerclass == "priest") {stam = 1, agil = 0, spell = 5, luck = 3;} if (playerclass == "Gift of the Warrior") {cout << stam << endl;} else if (playerclass == "gift of the warrior") {cout << stam << endl;} else if (playerclass == "Warrior") {cout << stam << endl;} else if (playerclass == "warrior") {cout << stam << endl;} else if (playerclass == "Gift of Rogue") {cout << stam << endl;} else if (playerclass == "gift of the rogue") {cout << stam << endl;} else if (playerclass == "Rogue") {cout << stam << endl;} else if (playerclass == "rogue") {cout << stam << endl;} else if (playerclass == "Gift of the Mage") {cout << stam << endl;} else if (playerclass == "gift of the mage") {cout << stam << endl;} else if (playerclass == "Mage") {cout << stam << endl;} else if (playerclass == "mage") {cout << stam << endl;} else if (playerclass == "Gift of the Priest") {cout << stam << endl;} else if (playerclass == "gift of the priest") {cout << stam << endl;} else if (playerclass == "Priest") {cout << stam << endl;} else if (playerclass == "priest") {cout << stam << endl;} } int main () { cout << "V0.0 \n\n" << "Welcome to Lights Deceit, a retro RPG where your intuition will be your guide. \n\n" << endl; cout << "Once their was a mighty Hero, in a war torn land. His journey began here... \n" << endl; cout << "You awaken feeling rested at the tavern. \n" << endl; cout << "You hear chatter and laughter outside the small room, and look up to meet\nthe sunshine on your face.\n\n"; cout << "You grasp the Stone on your neck, feeling the soothing warmth of its energy, what gift would it bestow you today?\n\n\n"; string action1 = ""; PlayerClass(); cout << "\nYou get up and look around, you see a chest, window, and door.\n" << endl; getline(cin, action1); if(action1 == "open chest") {cout << "You find your rusty knife and clothes and gear up to set out on an adventure.\n"; PlayerStats();} if(action1 == "open window") {cout << "You peek your head out the window and peer into the distance";} if(action1 == "open door") {cout << "You step out into the tavern, theirs grubby men, beautiful ladies, and everything in between." << endl; cout << "Near the barkeep a fight breaks out, a man in a tattered jacket sneaks up with a small shank to take advantage of the opportunity" << endl; cout << "You think to yourself, could I attack him?\n Would it even be worth it?..\n"; cout << "I could just run and be on with my adventure, or perhaps I could wait and take the thief after hes had his way with his victims";} return 0; }``````
Last edited on
Well it returns 0 because in line 146, you have 'return 0'. What do you want that function to return? the type of that function is 'int' and I don't think you want it to return 0, do you?
You have several other problems:

Line 50: spec is an uninitialized variable. Your comparisons at lines 56,59,62,65 are going to result in undefined behavior.

Lines 51-54: stamina, agility, intellect, luck are local variables and go out of scope at line 86.

Line 96-99: These are also local variables that go out of scope at line 170.

Line 178: Actions1() is not defined.

Thank you my man, you helped me understand, not working yet but seriously helped me out. Was up waay to long my mind was mush thats where the undefined variable came from :p But seriously thanks dude
Okay I dont get whats wrong with spec, it gets initialized with cin >> spec; then after cout outputs, works the same as all my other variables
Last edited on
MoreUmph wrote:
Okay I dont get whats wrong with spec, it gets initialized with cin >> spec

Your variable `spec` is only initialized by `std::cin` at line 35 after your `if/else` block, lines 23-33.

Move line 35 up so it executes before the `if/else` block.
Last edited on
Thank you, dont know how I misunderstood that. Any advice on rerunning the code to look around the room again after opening the chest?
The easiest way is a simple loop.
 ``123456789`` `````` bool chest_opened = false; do { // look around the room (ideally, call a function). if (action1 == "open chest") { chest_opened = true; } } while (! chest_opened);``````

Topic archived. No new replies allowed.