Hey, I was messing around and thought I would try and build a text based RPG to test the things I've learned. I've encountered an issue with a public variable in one of my classes - it works in the class as it's supposed to, but when I try and call it from main it returns as 0 (rather than what it was set to). Probably an easy fix, but I just can't seem to find the issue.
#include "mm.h"
#include <iostream>
#include "charselect.h"
#include "exp.h"
#include <iostream>
mm::mm()
{
cout << "Please choose one of the following options :\n\n";
cout << "1 for new game.\n";
cout << "2 for load game.\n";
cout << "3 to exit program.\n\n";
casecheck();
}
int mm::casecheck()
{
int mc;
cin >> mc;
switch(mc)
{
case 1:
{
cout << "-------------------------------------------------------------------------------- \n\n";
charselect cs;
cs.setchar();
break;
}
case 2:
{
cout << "Currently this program does not support a save/load feature.\n\n";
mm();
break;
}
case 3:
{
//supposed to be an exit code, but leaving it as is atm to work on other things.
return 0;
break;
}
default:
{
mm();
}
}
}
#include "charselect.h"
#include "warrior.h"
#include "mage.h"
#include "rogue.h"
#include <iostream>
#include <string>
usingnamespace std;
string role;
charselect::charselect()
{
}
int charselect::setchar()
{
cout << "We will begin by choosing what class of character you want to play.\n";
cout << "There are currently 3 classes to pick from : warrior, mage, and rogue.\n\n";
cout << "The warrior is a melee fighter who relies on his large amount of\n";
cout << "vitality and strength to defeat his foes.\n\n";
cout << "The mage is a spell caster who relies on his intelligence to\n";
cout << "quickly decimate enemy forces\n\n";
cout << "The assassin is a cunning fighter who utilizes strength and dexterity to.\n";
cout << "slice through enemy ranks.\n\n";
cout << "-------------------------------------------------------------------------------- \n\n";
cout << "Please choose the number of the class you would like to play :\n\n";
cout << "1. warrior\n";
cout << "2. mage\n";
cout << "3. rogue\n\n";
cin >> x;
switch(x)
{
case 1:
{
role = "warrior. \n";
x = 1;
break;
}
case 2:
{
role = "mage.\n";
x = 2;
break;
}
case 3:
{
role = "rogue. \n";
x = 3;
break;
}
default :
{
while (x < 1 || x > 3)
{
cout << "Please re-enter your class.\n";
cin >> x;
}
}
cout << "You have chosen the role of : " << role << endl;
}
}
Now if you look at main - lines 22 - 47, you can see the switch I made, which is supposed to use the x variable used to choose a character in the charselect class. However, every time I try to get that variable it comes back as 0 (where I need it to retain the value it had when the character select was made).