NOGDUS

Welcome to NOGDUS => Newbie Corner => Topic started by: xhunter on August 14, 2009, 07:27:08 PM



Title: idea in mind
Post by: xhunter on August 14, 2009, 07:27:08 PM
Hi to all;
So,after 3 days of  reading the cplusplus documentation I've just had an idea in mind , and I started coding something...actually it does nothing for now . So I don't know why am posting this anyway  ???

Code:
#include<iostream>
#include<string>
#include<sstream>

using namespace std;

#define N "\n";
#define X_HEALTH 100;

using namespace std;

class Knight
{
Knight(); // you had a lowercase k, and needed an uppercase k
Knight(int race, string weapon, int mana, int health);

~Knight;

public:

struct bag
{
int items;
string weapon;
string potions;
string armors;
};

int take_damage(int nb_damage);

//int attack(int damage_value, int *target);
// I assume you wanted to attack another Knight
int attack(int damage_value, Knight& target);

void potions(string potion_name, int value_potion);
void change_weapon(string weapon_name, int damage_value);
int check_health(int health);

private:
int race;
string weapon;
int mana;
int health;
int armor;
string potions;
string armors; // you missed a semi colon, I added it for you
}; // you missed a semi colon, I added it for you


// the attack function
int Knight::take_damage(int nb_damage) // you forgot an int
{
health -= nb_damage;

/*
switch(health)
{
case health<0:
health==0;
case health > X_HEALTH;
health==x_HEALTH;
}
*/

// your code above does not function whatsoever.
// the logic is
// if the health is less than zero, then make it zero

health = (health < 0) ? 0 : health;

// you need to return something. I chose to return health
return health;
}

//Knight::attack(int &target)
int Knight::attack(int damage_value, Knight& target);
{
//&target.take_damage(int nb_damage);
// the code above doesn't work

target.take_damage(int damage_value);
}

void Knight::potions(int value_potion) // you missed a void
{
health += value_potion;

if (health > X_HEALTH)
{
// health == X_HEALTH;
// the above code is a comparison. not an assignment!
health = X_HEALTH;
}
}

/*
getchar();
return 0;
}
*/
// I don't know at all why you have that there...its going to cause an error duh


P.S: If a member of this forum want to edit this or correct me, yo are more than welcom and thanks in advanced  ;D

Good night

(edited by Richard Marks)


Title: Re: idea in mind
Post by: Richard Marks on August 14, 2009, 10:35:06 PM
Looks like you are preparing for a battle type demo.

Good luck!

Learn your syntax more!


Title: Re: idea in mind
Post by: xhunter on August 15, 2009, 06:37:41 AM
wow ! your modification was awesome, so as your correction, thanks Richard, you're the best;

Talking about the getchar() , I used it instead of the system("PAUSE") ( I was using devc++ ) otherwise, the application will just terminate



Regards,greyfox


Title: Re: idea in mind
Post by: Richard Marks on August 15, 2009, 06:58:44 AM
You are welcome.

Use this instead of getchar() or system("pause"). It will work on any C++ compiler.

Place this in a header or at the top of your file (before the rest of your code, but after includes)
Code:
void better_pause()
{
    fflush(stdin);
    std::string betterpause;
    std::cout << "Press Enter to Continue...";
    std::getline(std::cin, betterpause);
}

and then when you need to pause, you just call it:
Code:
better_pause();

Much better than using non-portable functions. ;)
Remember, write cross-platform code and you will be writing better code! 8)


Title: Re: idea in mind
Post by: xhunter on August 15, 2009, 08:40:21 AM
bright Idea !  ;)

I should have thought about this ^^

Am learning a lot of things from you, thanks Richards


Title: Re: idea in mind
Post by: xhunter on August 15, 2009, 08:43:24 AM
by the way, what's the role of the fflush() function, is it important ?


Title: Re: idea in mind
Post by: Richard Marks on August 15, 2009, 09:27:24 AM
Read the fflush() documentation (http://www.cplusplus.com/reference/clibrary/cstdio/fflush/).