Jump to content

  • Log In with Google      Sign In   
  • Create Account


Can't get my if statements working in a function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Ryan Atkins   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2012 - 06:18 AM

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 [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";
	}
}


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11861

Like
2Likes
Like

Posted 18 February 2012 - 06:33 AM

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')
	  //...


#3 Ryan Atkins   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2012 - 06:37 AM

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.

#4 Álvaro   Crossbones+   -  Reputation: 11861

Like
0Likes
Like

Posted 18 February 2012 - 08:21 AM

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.

  • 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.


#5 Ryan Atkins   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2012 - 04:08 PM

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.

#6 Washu   Senior Moderators   -  Reputation: 4464

Like
1Likes
Like

Posted 18 February 2012 - 04:18 PM

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.
ScapeCode - Blog | SlimDX


#7 Alpha_ProgDes   Crossbones+   -  Reputation: 4680

Like
1Likes
Like

Posted 18 February 2012 - 04:35 PM

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. Posted Image
Beginner in Game Development? Read here.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#8 Washu   Senior Moderators   -  Reputation: 4464

Like
2Likes
Like

Posted 18 February 2012 - 04:55 PM

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 [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;
}

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.
ScapeCode - Blog | SlimDX


#9 Ryan Atkins   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 February 2012 - 06:46 PM

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

#10 Sepiantum   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 February 2012 - 09:40 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS