booleans

i read a tutorial on booleans from: http://www.cprogramming.com/tutorial/lesson2.html

and it sux... i didnt really understand anything about booleans can anyone show me an example? or a better tutorial? or just explain them a bit more?
By booleans do you mean something like bool done = false; or the logical operators like || && etc?
on the tutorial i was reading they were talking about logical operators,

What exactly don't you understand about them? The tutorial seemed to explain them well enough.
oh...well then perhaps i just need to see them played out are there some examples of their uses anywhere?
i read it a few more times....and i THINK im starting to get it...but i would still appreciate some examples of it in action
I'm only in my 6th week in C++, please don't be too harsh, this is my first program that uses a class.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef H_character
#define H_character
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

class character
{
public:
	void print() const;
	void setName(string);
	int doDamage();
	void takeDamage(int);
	bool isDead();
	string getName() const;
	character();
private:
	int strength;
	int health;
	int agility;
	string name;
};

void character::print() const
{
	cout<<"Name:         "<<name<<endl;
	cout<<"Strength:     "<<strength<<endl;
	cout<<"Agility:      "<<agility<<endl;
	cout<<"Health:       "<<health<<endl<<endl;
}

int character::doDamage()
{
	int factor = (rand() + time(0)) % 3;
	return ((strength * agility) / 10) * factor;
}

bool character::isDead()
{
	if(health <= 0)
		return true;
	return false;
}

character::character()
{
	name = "";
	strength = 10;
	agility = 10;
	health = 200;
}

void character::setName(string input)
{
	name = input;
}

void character::takeDamage(int num1)
{
	health = health - num1;
}

string character::getName() const
{
	return name;
}
#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "character.h"

using namespace std;

int main()
{
	int xxx;
	int num1, num2;
	string str1;
	character player1, player2;
	cout<<"Enter name for player 1: "<<endl;
	cin>>str1;
	player1.setName(str1);
	player2.setName("Computer");
	while(player1.isDead() == false && player2.isDead() == false())
	{
		num1 = player1.doDamage();
		player2.takeDamage(num1);
		cout<<player1.getName()<<" rolls a "<<num1<<endl;
		cout<<player2.getName()<<" receives damage."<<endl;
		if(player2.isDead() == true)
			break;
		player2.print();
		cin>>num2;
		num1 = player2.doDamage();
		player1.takeDamage(num1);
		cout<<player2.getName()<<" rolls a "<<num1<<endl;
		cout<<player1.getName()<<" receives damage."<<endl;
		if(player1.isDead() == true)
			break;
		player1.print();
		cin>>num2;
	}
	if(player1.isDead() == true)
		cout<<"Computer wins...."<<endl;
	else
		cout<<endl<<player1.getName()<<" wins!!!!"<<endl;
	cin>>xxx; // variable to view output
	return 0;
}
Topic archived. No new replies allowed.