Can't get my if statements working in a function

Started by
8 comments, last by Sepiantum 12 years, 2 months ago
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?

My header file is also empty right now.

main.cpp


#include <iostream>
#include "functions.h"

using namespace std;

void classChoice();

int main()
{
classChoice();
return 0;
}



classes.cpp
#include <iostream>
#include "functions.h"
int str, dex;
void warrior()
{
str = 8;
dex = 5;
}void assassin()
{
str = 3;
dex = 10;
}
void barbarian()
{
str = 6;
dex = 7;
}using namespace std;
void classChoice()
{
cout << "Behold... Anternia waits before you. Please choose your playstyle from the following. The choices are [W]arrior, [A]ssassin, and arbarian" << endl;
char playerClass;
cin >> playerClass;
if(playerClass == 'W' || 'w')
{
warrior();
cout << "Warrior description";
}
if(playerClass == 'A' || 'a')
{
assassin();
cout << "Assassin description";
}
if(playerClass == 'B' || 'b')
{
barbarian();
cout << "Barbarian description";
}
}
Advertisement

if(playerClass == 'W' || 'w')


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]

  1. Don't get in the habit of using global variables.
  2. 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'.
  3. You really shouldn't get in the habit of using global variables.
Is there a whole other way to make the str and dex modifiers without using extern int? I'll look up lessons if you just point me in the direction.
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.


void warrior()
{
str = 8;
dex = 5;
}void assassin()
{
str = 3;
dex = 10;
}
void barbarian()
{
str = 6;
dex = 7;
}

These should really be classes not functions. I suggest you read up on classes (very heavily) and post here if you have any more questions. smile.png

Beginner in Game Development?  Read here. And read here.

 

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.

Thank you both. I'm going to spend my night figuring out classes.

if(playerClass == 'W' || 'w')


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

This topic is closed to new replies.

Advertisement