So I started to write a beginners text based game to solidify the stuff I've learned so far. I wrote this as the main.cpp and it worked fine. When I wanted to use all of this as a function by moving to a separate source file and renaming int main() to void classChoice(), all the if statements would run no matter what I entered. What can fix this?
Operator || takes two boolean statements. It will evaluate the first one (playerClass == 'W') first. If it is false, it will evaluate the second one ('w'). Confusingly enough, 'w' is understood as a small number (a char) with a value of 87. You are then trying to determine if 87 is true or false, and in C++ any number that is not 0 is considered to be true. So your expression ends up being always true.
This is what you meant to say: if (playerClass == 'W' || playerClass == 'w')
//...
This absolutely worked. Though, I still don't understand why the whole thing worked before I moved it to another file. Is it just the compiler giving me leeway?
I also now have the problem where the declared variables won't transfer their values over to the main.cpp.
This absolutely worked. Though, I still don't understand why the whole thing worked before I moved it to another file. Is it just the compiler giving me leeway?
You must not be remembering correctly what code you had before. There is no way that `if' statement could have worked under any circumstances.
I also now have the problem where the declared variables won't transfer their values over to the main.cpp.
[/quote]
Don't get in the habit of using global variables.
If you do want to use a global variable, it should be defined only in one module and it should be declared with `extern' in the other modules that want to use it. This is typically done in a header file that the other modules `#include'.
You really shouldn't get in the habit of using global variables.
structures and classes is one way. You could also simply use references.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Here's a very basic example of what you could do. It's terribly basic, and not the best approach, but it does demonstrate some of the basic concepts.
#include <iostream>
#include <string>
struct Character {
Character(std::string const& className, int str, int dex) : className(className), str(str), dex(dex) {}
std::string className;
int str;
int dex;
};
Character choose_class() {
char choice;
std::cout<<"Pick your class: [W]arrior, [A]ssassin, or arbarian.";
std::cin>>choice;
while(true) {
switch(choice) {
case 'w':
case 'W':
return Character("Warrior", 8, 5);
case 'a':
case 'A':
return Character("Assassin", 3, 10);
case 'b':
case 'B':
return Character("Barbarian", 6, 7);
default:
std::cout<<"Invalid choice, valid choices are [W]arrior, [A]ssassin, or arbarian."<<std::endl;
}
}
}
int main() {
Character c = choose_class();
std::cout<<"You have chosen the "<<c.className<<"."<<std::endl;
}
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
That is a problem. You are testing if playerClass is equal to the character 'W', or if the integer value of 'w' is not 0. The integer value of 'w' is never zero and never will be zero, so each if statement will run. Change it to: if(playerClass == 'W' || playerClass == 'w')
Follow and support my game engine (still in very basic development)? Link