Advertisement

Just started learning C++, please help!

Started by July 12, 2002 01:18 PM
13 comments, last by Manny Calavera 22 years, 5 months ago
I'm twelve years old and I've just started learning C++ by reading C++ For Dummies. So far, I understand all that is being presented before me...except one thing. Those Boolean things.If you have C++ For Dummies on page 47, it says Logical Variable Types. And I just don't understand it. What is a "Boolean"? Let me type up the paragraph and hopefully you could translate in even more lamest terms (I'm a real dummy ):
quote: Logical Variable Types If > is an operator, then a comparison such as a>10 must be an expression. Clearly, the result of such and expression must be TRUE or FALSE (I understand this, the next parts I do not understand clearly). You may have noticed already that there was no Boolean(???)variable type mentioned in our discussion of variable types back in Chapter 2. That is, there is no variable type that can have the value TRUE or FALSE. Then what is the type of an expression such as a>10? C++ uses the type int to store Boolean values. The value 0 is taken to be FALSE. Any value other than zero is TRUE. An expression such as a>10 evaluates to 0 (FALSE) or 1 (TRUE. WARNING: Microsoft Visual Basic also uses an integer to hold TRUE and FALSE values; however, in Visual Basic, a comparison operation returns either a 0 (FALSE) or a -1 (TRUE). The new ANSI C++ standard does define a type bool to handle Boolean variables; however, it is not supported in the GNU C++, which comes on the enclosed CD-ROM.
Could you pleeeease translate this section, one paragraph at a time? I have the Microsoft Visual C++ compiler if that is of any help. Could you also tell me WHEN I'll be using this type of stuff? Thanks a lot! [edited by - Manny Calavera on July 12, 2002 2:40:28 PM]
A boolean variable in C++ holds only one of two values: true or false. Older versions of C/C++ (before standardization) didn''t have a built-in boolean variable; all non-zero values were considered true and 0 was considered false.

Nowadays just about every major compiler comes with a variable bool as a primitive type.

bool x = true;
x = false;
if(x == true)
x = false;
else
x = true;

Older compilers had to make their own boolean type using defines or typedefs, usually in a header named bool.h.

typedef int BOOL
// or #define BOOL int
#define TRUE 1
#define FALSE 0

That book of yours must be REALLY old. I''d suggest getting a more up to date compiler... try DJGPP. It''s a free DOS32 compiler.

--------------------------
"Facing terror isn''t half as fun as sharing it" -- Bun-bun
--------------------------{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }
Advertisement
The book is copyrighted in 2000. Is that really old for a C++ book?

Anyway, yeah, maybe I should update my Visual C++ compiler.

BTW, I don''t really want to sound like a fool but I didn''t understand what you meant lol.
quote: Original post by Manny Calavera
BTW, I don''t really want to sound like a fool but I didn''t understand what you meant lol.


heh.

quick translation: in C++ there are lots of variable types that can hold different kinds of information.

int holds only whole numbers like: 1,2,5,10,-3

float holds decimal numbers like: 1.0, 3.442423, 0.0324

bool is a variable type that holds boolean values. boolean means either true or false. those are the only values that it can hold: TRUE, or FALSE. once you get further in the book you will start to understand why boolean variables are useful.

-me

and further in the book, will it teach me how to use Booleans? I mean, how can I integrate it in programs? Do I type bool a>10 or something? I''m really lost lol
I''d stay away from DOS stuff if I were you. Borland offers a free version of their compiler. It has a command-line interface like every other compiler, but it''s Win32, not DOS. Two other good options are LCC-Win32 and MinGW. Both are ports of GCC (from the Unix world) to Win32. I happen to prefer MinGW because I find it to be more powerful and a more pure form of GCC, but LCC might be a little easier for starters. Probably, though, both of these may be a bit difficult to configure and use if you''re not used to compilers already. I''d use the Borland compiler if I were in your situation.
Advertisement
Couldn''t you also use booleans as flags of events that have happend?

I know in Visual Basic you can.
When the world has got you down, that's when you strike back and show them who you really are.
First of all, your book is outdated - all recent C++ compilers worth mentioning supports the bool type. You use this data type when you need to declare a variable that only will hold one of two values (true or false), just like you would use int when declaring a variable to hold integers. For example:

int  invaders_left    = 100;bool planet_destroyed = false; 

Boolean values (true and false) can be converted to and from integers. In C++, the value true will be converted to the integer value 1 while the value false will be converted to 0. That's not necessarily so in other languages (like Basic dialects). When converting from integers to boolean values, zero is considered false while everything non-zero is considered true.

If you add two integers together, like a + 10 (assuming a is an integer variable), the result will be an integer value. When you compare two values with each other, like a > 10, the result is a boolean value - the result is the answer to the question "is a greater than 10", which must be either "true" or "false".

Hope that helps.



[edited by - spock on July 12, 2002 3:10:42 PM]

[edited by - spock on July 12, 2002 3:16:07 PM]
Let me help try to explain this too you as simple as possible.

Visual C++ does actually support bool type. The compiler that actually came with the book does not. It is best though not to use it unless you will always stay on that same compiler because some compilers still do not support it.

Boolean is a type of math that deals with only true and false.

I will type out and if statement for you and then break it down into parts.

if(x>5)
{
cout << "x is greater than 5" << endl;
}
else
{
cout << "x is less than or equal to 5" << endl;;
}


first what the program does is takes (x>5) and evaluates it. So say x is equal to 10 then it would be (10>5). Is that statement true or false. Well obviousely it is true. So since it is true the computer returns a non-zero int. int being an integer type. Now say x is equal to 3. Well than you have (3>5) is that a true statement, is 3 greater than 5? No. So since that is not true than the computer returns 0. Since 0 is returned it is false and does not print to the screen "x is greater than 5" instead it prints "x is less than or equal to 5". Boolean is an extremely important part of programing.

It is needed to give a program some direction. Otherwise all you program does is execute a series of commands no matter what the program must do. If you are making a game and you want the person to pick a number between 1 and 10 where each number does something. You need to as the computer if the person put in a number between 1 and ten and if they did what number did they input. You need boolean to be able to do this. So you need to understand it to move on. Don''t wrry about understanding it completely just understand what true and false are and that 0 is false and that anyother number is true. If you know that then you are good to go on. It will make more sense to you later on.

Also I would maybe recommend a different language to learn first like visual basic or java becuase they are easier. When you get to pointers and refrences it will start to get really confusing. Those deal with memory managment.

Good luck. Good to start young to. I did and I had trouble understanding it then, but now I can teach myself languages with ease.
quote: Original post by Manny Calavera
and further in the book, will it teach me how to use Booleans? I mean, how can I integrate it in programs? Do I type bool a>10 or something? I'm really lost lol


yes it will teach you how to use booleans. don't worry about how to integrate them into programs till you get there. right now the point of where you are in the book is to introduce you to the basics of the language. later it will tell you how to use those basics to make programs.

trying to figure it all out right now will just confuse the hell out of you.

if could tell you that you use booleans as escape sequences of for loops or while loops. or that booleans are super handy for if/else statements or to represent flags in classes but since you haven't learned any of those things yet, you'll only get super confused. for now i'd really suggest that you just stick with the path of the book for a few days longer and it'll start making more sense.

[EDIT: took out potentiall obnoxios sentance, sorry, my boss is a bitch today]

for now, just know that booleans hold either TRUE or FALSE as values and that they ARE usefull but you won't find out how until later.

anyway, let us know if there are more questions.

-me



[edited by - Palidine on July 12, 2002 3:25:56 PM]

This topic is closed to new replies.

Advertisement