Sign in to follow this  
Ryan Atkins

Can't get my if statements working in a function

Recommended Posts

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
[code]

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

using namespace std;

void classChoice();

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

[/code]

classes.cpp
[code]#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 [B]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";
}
}
[/code]

Share this post


Link to post
Share on other sites
[quote name='Ryan Atkins' timestamp='1329567492' post='4914166']
if(playerClass == 'W' || 'w')
[/quote]

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:
[code] if (playerClass == 'W' || playerClass == 'w')
//...[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Ryan Atkins' timestamp='1329568635' post='4914173']
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?[/quote]
You must not be remembering correctly what code you had before. There is no way that `if' statement could have worked under any circumstances.

[quote]I also now have the problem where the declared variables won't transfer their values over to the main.cpp.
[/quote][list=1]
[*]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.
[/list]

Share this post


Link to post
Share on other sites
[CODE]
void warrior()
{
str = 8;
dex = 5;
}void assassin()
{
str = 3;
dex = 10;
}
void barbarian()
{
str = 6;
dex = 7;
}
[/CODE]
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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
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.

[CODE]
#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 [B]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 [B]arbarian."<<std::endl;
}
}
}

int main() {
Character c = choose_class();
std::cout<<"You have chosen the "<<c.className<<"."<<std::endl;
}
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='Ryan Atkins' timestamp='1329567492' post='4914166']
[code]if(playerClass == 'W' || 'w')[/code]
[/quote]

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:
[code]if(playerClass == 'W' || playerClass == 'w')[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this