CharacterCreation::CharacterCreation()
{
int warrior, mage, rogue, priest;
int class1;
A constructor is very similar to any other kind of function. Just like regular functions, you can declare local variables which are not accessible outside the function. That's what you've done here.
Here's a simplified example of the same problem:
#include <iostream>
void foo() {
int aNumber;
std::cout << "Enter a number! ";
std::cin >> aNumber;
}
int main() {
std::cout << "You entered the number " << aNumber << "\n";
}
Computer programs can be really big and really complex, which makes them really hard to make sure they keep working as new features are added and bugs are fixed. So one of the most critical techniques to avoiding complexity, and to make sure programs are as easy as possible to work on, is to isolate every part of a program as much as possible from other parts of the program, and to tightly control how parts of programs interact with one another.
Languages support this in different ways. One of those ways is the concept of variable scope, or the region of a program that's allowed to interact with a variable. In particular C++ limits the scope of a variable in a function so that nothing outside that function can get at it.
When you do want to have different parts of a program interact, there are many, many ways to do this and it doesn't make much sense to try listing them. Each different method will be suited to different programming goals, so it really depends on what you're doing.
I don't know exactly what your goals are, but having a CharacterCreation
class and taking user input in the constructor seems like a dubious design, and it's probably over-complicating things. That said, here's a way to make the program work according to the apparent intent:
// CharacterCreation.h
#ifndef CHARACTERCREATION_H
#define CHARACTERCREATION_H
class CharacterCreation {
int classID;
public:
CharacterCreation();
int getClass();
};
#endif // CHARACTERCREATION_H
// CharacterCreation.cpp
#include "CharacterCreation.h"
#include <cstdlib> // abort()
#include <iostream> // cout, cin, cerr
CharacterCreation::CharacterCreation() {
std::cout << "Choose a class:\n[1] Warrior\n[2] Mage\n"
"[3] Rogue\n[4] Priest\n\n";
std::cin >> classID;
if (std::cin.fail()) {
std::cerr << "Error: failed to get user input.\n";
throw std::runtime_error("input failed");
}
if (classID < 1 || 4 < classID) {
std::cerr << "Error: invalid user input.\n";
throw std::runtime_error("invalid selection");
}
switch (classID) {
case 1:
std::cout << "Learned Sword Combat!\n\n";
break;
case 2:
std::cout << "Learned the Arcane Arts!\n\n";
break;
case 3:
std::cout << "Learned the Art of Assasination!\n\n";
break;
case 4:
std::cout << "Learned the Art of the Divine!\n\n";
break;
default:
abort();
}
}
int CharacterCreation::getClass() { return classID; }
// main.cpp
#include "CharacterCreation.h"
#include <cstdlib> // abort()
#include <iostream> // cout, cin, cerr
int main() {
CharacterCreation cc;
switch (cc.getClass()) {
case 1:
std::cout << "You chose warrior\n";
break;
case 2:
std::cout << "You chose mage\n";
break;
case 3:
std::cout << "You chose rogue\n";
break;
case 4:
std::cout << "You chose priest\n";
break;
default:
abort();
}
}
If I just wanted to write a program with the same output the I might instead write:
// main.cpp
#include <iostream>
#include <array>
struct CharacterClass {
char const *name;
char const *output;
};
int main() {
std::array<CharacterClass, 4> classes = {
{{"Warrior", "Learned Sword Combat!\n\nYou chose warrior"},
{"Mage", "Learned the Arcane Arts!\n\nYou chose mage"},
{"Rogue", "Learned the Art of Assasination!\n\nYou chose rogue"},
{"Priest", "Learned the Art of the Divine!\n\nYou chose priest"}}};
std::cout << "Choose a class:\n";
for (int i = 0; i < classes.size(); ++i) {
std::cout << "[" << i + 1 << "] " << classes[i].name << "\n";
}
int classID;
std::cin >> classID;
if (std::cin.fail()) {
std::cerr << "Error: failed to get user input.\n";
throw std::runtime_error("input failed");
}
if (classID < 1 || classes.size() < classID) {
std::cerr << "Error: invalid user input.\n";
throw std::runtime_error("invalid selection");
}
std::cout << classes[classID - 1].output << '\n';
}