Important alert: (current site time 7/15/2013 4:42:33 PM EDT)
 

article

A C++ Tutorial for Complete Beginners #2

Email
Submitted on: 10/18/2002 8:55:02 PM
By: Jared Devall 
Level: Beginner
User Rating: By 67 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 83323
author picture
(About the author)
 
     This is the followup tutorial to "A C++ Tutorial for Complete Beginners #1". This article explains Variables!


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				


Well, Hello World, Again! It's been quite a while since my previous Tutorial; time for a new one!

This tutorial will assume you've read " A C++ Tutorial for Complete Beginners #1" as this is #2. This tutorial will also assume you understand the concepts presented in the previous tutorial and can make use out of it. :)

*Note: I've since changed to using MS Visual C++ 6.0

Notes:
- Blue denotes a keyword
- Term Dictionary still not in use. ( Links don't work. )

I'm going to cover variables in this article. So, get ready! :)

Variables:
Variables are aliases for memory locations. That is, they hold some kind of data that you put inside of it. You declare a variable by choosing a data type, and choose a name or keyword to assign to the variable. There are, however, reserved KEYWORDS that you cannot use, as they are critical to C++ Programming.

A list of Data Types:
  • char - 1 byte - This represents ONE character. e.g. b
  • int - 2 or 4 bytes - This for a number. e.g. 500 or -500
  • short - 2 bytes - This is also a number, but is smaller than int.
  • long - 4 or 8 bytes - This is the largest number type.
  • bool - 1 byte - This is a boolean value. ( true or false )
Data types can change from system to system, but these are the 'normal' sizes for these types. There can be both signed, and unsigned numbers. Signed meaning the number can be negative, and unsigned meaning the lowest value the variable can have is 0.

Size Sign Minimum Value Maximum Value
1 signed -128 127
1 unsigned 0 255 (normal char)
2 signed -32768 32768 (normal short/int)
2 unsigned 0 65535
4 signed -2147483548 2147483647 (normal long)
4 unsigned 0 4294967295

Types default to signed unless you specify that it is unsigned, like so:
unsigned int

C++ is a very CASE-SENSITIVE language. What this means is that if you have 3 variables:
TestVariable
testVariable
testvariable


These variables are different. It's important to develop a consistent style of naming variables. I normally keep variables lowercase, with an uppercase specifying a new word, like so:
testVariable

Now that you know what data types and variables are you can declare them. You declare them like so:
int myNumber; *Note: You must have the semi-colon at the end!
You can also define them in their declaration:
int myNumber = 2002;

Char's are done close to the same way, except when you define them you must put the letter in single quotations ( ' ). Character strings, however, must be done a different way since the char type can only hold ONE character at a time. ( Yes, that mean it can only hold one A or one B at a time! ). Strings will be discussed in a later tutorial.

A bool(ean) can only be true or false, or their counterparts: 0 and 1. Booleans are normally used in if statements and other C++ goodies.

Okay, wipe that sweat from your brow! This was a difficult and large chunk of information to swallow. Let's end this up with a small program that utilizes what was presented here.


Open up whatever program you used in the last tutorial and follow the steps on compiling I showed you before. Again, remove the numbers from the source code as they are only there to help analyze the code! Save as tutorial.cpp


1. #include
2.
3. int main( )
4. {
5. int myNumber;
6. long myNumber2 = 5;
7. char myCharacter;
8. char myCharacter2 = 'c';
9. bool myBoolean;
10. bool myBoolean2 = true;
11.
12.
13. myNumber = 3678;
14. myCharacter = 'a';
15. myBoolean = false;
16.
17. cout << "myNumber = " << myNumber << endl;
18. cout << "myNumber2 = " << myNumber2 << endl << endl;
19.
20. cout << "myCharater = " << myCharacter << endl;
21. cout << "myCharacter2 = " << myCharacter2 << endl << endl;
22.
23. return 0;
24. }


Okay, now, the line-by-line analysis!

Line 1: This includes the iostream.h header. This, ofcourse, is needed for cout and endl.

Line 2: This is just whitespace.

Line 3: This is the main function mentioned in the last tutorial. You will always see one of these in any C++ program.

Line 4: This is just the opening brace for the main function.

Line 5 & 6: These two lines declare one int and one long variable. myNumber2 is also defined on this line with the value 5.

Line 7 & 8: These two lines both declare a char variable. myCharacter2 also defines itself with the value c.

Line 9 & 10: These two lines both declare a bool variable. myBoolean2 also defines itself to true

Line 11 & 12: Both of these lines are cosmetics: a.k.a. whitespace.

Line 13 - 15: These lines define these variables to a specific value.

Line 16: Whitespace again!

Line 17 & 18: These lines display the variables' values to the screen, sending an 'endl' to go to the next line.

Line 19: Guess what this is! Yup, you guessed it, whitespace!

Line 20 & 21: These lines also display the variables' values to the screen, sending an 'endl' to the next line.

Line 22: ...whitespace...

Line 23: This is the return statement. This will be explained in the next tutorial, along with the mystical "function".

Line 24: This is simply the closing brace. This ends the function, and effectively ends our program. ( When main is done, our program is done! )


For ease of explanation, whitespace will not be mentioned in future tutorials. I think we all know what it is. :)

This program simply declares and defines a few simple variables. It then takes those variables with printable values and prints them to the screen with cout. Again, this program will most likely close immediately upon execution. Just open up some kind of command-line and run it from there.

This concludes "A C++ Tutorial for Complete Beginners #2". As always, if anything is too complicated to understand or if I screwd up, be sure to let me know. Please let me know if you would like something explained a little more indepth, or whatever else. ( Please have it pertain to the topics discussed in this tutorial. ) I'll try to get the fixes when I can!

- Jared


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/19/2002 10:11:26 AM[SpiderMan]

Perfect work! Just like the ver #1 of this tutorial, only that this one is better :) 5 globes again!
(If this comment was disrespectful, please report it.)

 
10/19/2002 10:32:41 AMThorgrim

Nice tutorial :)
I really hope you will continue writing !
(If this comment was disrespectful, please report it.)

 
10/19/2002 7:10:10 PM

Thank u
now I want programs about OOP
and Application project
(If this comment was disrespectful, please report it.)

 
10/19/2002 7:20:17 PM

Thank u Jared Devell
I want to level 2 or medile level
and some Application Project
other time thank u

(If this comment was disrespectful, please report it.)

 
10/22/2002 6:44:58 AM

Thanx for your help bro!
(If this comment was disrespectful, please report it.)

 
11/9/2002 2:38:16 AM

I'm using these tuts of yours to learn c++ and these are great. I've given both 5 globes.
Would you be able to write a tutorial on calling a routine from inside the main() routine?
I was just trying to see if I could do so, but I cannot figure it out without a bit of help.
(If this comment was disrespectful, please report it.)

 
11/14/2002 4:18:09 AMJosé Pablo Ramírez Vargas

I can easily see that you put a lot of effort in doing this. It is very good.

I am a VB programmer and started with C++ a few days ago. I understand most basic parts of the language now, but there are still things I don't get.

For example, what's the difference between using a compiled header file, or not using one? Or what does "extern" do? Or, in general, a tutorial on preprocessor keywords.

Or something about structures. What is the difference between these syntaxes?

1. struct struct-name
{ list-of-members
};

2. typedef struct [tag]struct-name
{
list-of-members
} tag-modifiers;

What is the tag all about? Also, a tutorial on typedef would be good. Anyway, just a few ideas for tutorials. :)
(If this comment was disrespectful, please report it.)

 
11/16/2002 11:16:27 PMPatabugen

thanks again! Looking forward to the next one!
(If this comment was disrespectful, please report it.)

 
1/8/2003 11:09:55 PM

I think it's good so far.
(If this comment was disrespectful, please report it.)

 
1/23/2003 10:15:11 PM

Hi there! Thanks for all these helps pages. I've been taking C++ since the 2nd quarter in school. Do you know how to write a program to convert binary number to decimal and decimal to binary? If so, can you please send it to my email? Thanks a lot!

I'm using Microsoft Visual C++.
(If this comment was disrespectful, please report it.)

 
2/5/2003 8:24:53 PM

Can you send me a program microsoft visual C++ to me by email thanks to your
tutorial,I want to try out that program
so can you send it by email?
(If this comment was disrespectful, please report it.)

 
2/6/2003 9:12:21 AM

It would be good to mention that declaring variables usually assignes them by default. This default-value can be compiler-dependent (usually 0 for integers). Using unassigned variables is a common cause of unpredicted program behavior. I suggest outputting some variables before the first assignement, to see the defaults in this example.
(If this comment was disrespectful, please report it.)

 
2/13/2003 2:55:32 PM

hmm is it me or the 2 boolean variable are useless?
nice job :)
(If this comment was disrespectful, please report it.)

 
2/20/2003 9:55:57 PM

Jared... I am having a difficult time understanding the coding for the variables. At one point myNumber is defined as an int (which I thought had only one character), but then it is defined as a four-digit number. In either case, the four digit number does not show up when I run the program. Am I missing something? Also, you described
(If this comment was disrespectful, please report it.)

 
2/20/2003 9:57:51 PM

Also, you set the long variable to "5", but I thought this was for 4-8 characters. Could you please help me understand?
(If this comment was disrespectful, please report it.)

 
3/2/2003 1:15:05 PM

ok im completely lost. i tried it your origional way as you showed and im getting these errors:
"C:/Dev-Cpp/include/c++/bits/stl_alloc.h: At global scope:
C:/Dev-Cpp/include/c++/bits/stl_alloc.h:575: syntax error before `;' token
C:/Dev-Cpp/include/c++/bits/stl_alloc.h:575: confused by earlier errors, bailing out

Execution terminated"

any help?
(If this comment was disrespectful, please report it.)

 
3/6/2003 1:30:28 AM

Very good, better than your first one. I'm learning *beams*

One problem. In line 1, you failed to mention what #include is including. I got an error and had to refer to your original tutorial. I don't mind, it was good practice for me, but others might be frusturated.
(If this comment was disrespectful, please report it.)

 
3/22/2003 1:28:02 AM

Great tutorial! and with a name like DEVall ;) it must be easy. 5 globes.
(If this comment was disrespectful, please report it.)

 
4/22/2003 5:09:47 PM

you need to write a book. i like the way you explain what your doing and why
(If this comment was disrespectful, please report it.)

 
5/3/2003 10:53:29 PMSarafraz Singh Johl

Correction: Range of Signed Integers is from -32768 to 32767.
(If this comment was disrespectful, please report it.)

 
7/18/2003 1:25:46 PM

Ah your tutorials are awesome! I acually learning C++ =D =D =D
(If this comment was disrespectful, please report it.)

 
8/6/2003 10:03:31 AM

How do you compile in Visual C++? I keep getting strange errors I don't get in DEV.
(If this comment was disrespectful, please report it.)

 
8/7/2003 5:15:34 PM

none of the bool stuff is working for me... bool doesnt even turn blue
????????????????????????????
(If this comment was disrespectful, please report it.)

 
8/7/2003 6:43:49 PM

nevermind, i just needed the new version

note: u forgot after #include in your example
(If this comment was disrespectful, please report it.)

 
8/17/2003 9:55:34 AM

Man im really lost in all this. I want to make a video game and im trying to figure out how to use this c++ stuff. can u email me at blisteringbunz@hotmail.com and explain how i can use this to make a game? thx
(If this comment was disrespectful, please report it.)

 
9/25/2003 1:06:51 AMJared Devall

Answers and Questions to the Tutorial & Comments! Ask and Answer: www.yuidesigns.net/~rendelven
(If this comment was disrespectful, please report it.)

 
9/30/2003 4:00:25 PM

I still got this odd problem... Even though the program works, it runs itself in DOS, it doesn't freeze itself but it doesn't show the text. Also, you forgot to add the "iostream.h" after the #include. But again, good job on it.
(If this comment was disrespectful, please report it.)

 
11/12/2003 1:30:26 AM

i guess c++ really is case sensitive im not really clear on variables but im gonna keep on
(If this comment was disrespectful, please report it.)

 
11/17/2003 1:51:28 PMJames Dunne

Great Tutorial Jared 5 Globes, Helped me alot!
(If this comment was disrespectful, please report it.)

 
11/18/2003 6:19:21 PM

thanks, it helped me alot:)
(If this comment was disrespectful, please report it.)

 
12/1/2003 6:09:03 PM

Thanks for this great starting boost. I have one question, though. When I put in the system ("PAUSE") after cout, it doesn't have "Hello World!" displayed, only "press any key to continue". You said something about a command line. I have a command prompt tool (A.K.A. MS-DOS). Please tell me what to put into it in order to run the program. Thank you very much.
(If this comment was disrespectful, please report it.)

 
6/12/2004 3:13:02 AM

It's good. However, Jared should check his code syntax for errors.
e.g. in the first line, he did not write
He also wrote myCharater in one other line, instead of myCharacter.
(If this comment was disrespectful, please report it.)

 
3/11/2005 12:57:42 AM

the devcpp-4.9.9.2-src file from tutorial #1 is all just a bunch of files my computer can not read, please help me,
j
(If this comment was disrespectful, please report it.)

 
3/15/2005 1:10:15 AM

i have it is simply a great program
but he must have checked it for error he had done at few locations of the code but over all the program is great
good work jared **Best of luck**
(If this comment was disrespectful, please report it.)

 
3/29/2005 2:21:20 PM

Dude in line 17 i got an ERROR, it says
17 C:\My Documents\hello.cpp `cout' undeclared (first use this function)
(If this comment was disrespectful, please report it.)

 
7/6/2005 2:15:50 PM

please make a third tutorial, please
(If this comment was disrespectful, please report it.)

 
8/25/2005 7:36:08 AMshakeel

WHERE CAN I DOWNLOAD THE CODE ? I AM NOT ABLE TO SEE ANY DOWNLOAD LINK.
(If this comment was disrespectful, please report it.)

 
9/15/2005 7:53:37 PMel

Hi Jared,
I love the way you present your ideas.
Please could you help me with writing some programs, and then eplaining each of them as simple as you've always done?
Thanks so much.
Please I expect an urgent reply.
Thanks so much.
El
(If this comment was disrespectful, please report it.)

 
1/24/2006 6:40:47 AMjeffrey

hi, im brand new to c++ and these r the first tutorials i have read. if it could be at all possible for someone to tell me the codeing to slow down the run sequence so i coulda see what was happening i would appreciate it greatly. these are great and i plan to use them much more, as well as any advice anyone from this site can offer, to learn c and c++ (they are pretty much the same arnt they?)

jeffrey
(If this comment was disrespectful, please report it.)

 
4/1/2006 3:14:37 PMStuart clark

Well when I put the code into the dev-C++, and I try to compile it there is something wrong with the couts in line 17&18 also 20&21 it says its undeclared. IF you could help with that. That would be wonderfull.
(If this comment was disrespectful, please report it.)

 
4/10/2006 4:58:44 PM

thanks for another great tut.
(If this comment was disrespectful, please report it.)

 
7/27/2006 9:05:05 PMZack

I started your tutorials today and the first one went perfectly. However, when i tried to compile the code from the second one, it wouldnt do it. Then, noticing the iostream.h thingy wasnt there, i added it, and it compiled. Also, i really would like to actually be able to SEE these programs. Hahahaha.

Anyways, thanks for another awesome tutorial.
(If this comment was disrespectful, please report it.)

 
9/29/2006 12:46:55 PMRobert

Jared: I think you may have 4gotten the on this tutorial. Also, tried running it with VS C++ 2005 Express and get an error on "line 17" "undeclared identifier". Can you help, please???
(If this comment was disrespectful, please report it.)

 
3/27/2007 3:52:32 PMsilver

Ya I real like to slow the program down, so I can see it. Plus u were saying "Just open up some kind of command-line and run it from there." What did u mean by this? ;-) thanks 4 the help.

(If this comment was disrespectful, please report it.)

 
4/13/2007 3:21:08 PMDonkey

This tut of you is also very good.
I'm reading all your tutorials right now, they're very learnful. :)
(If this comment was disrespectful, please report it.)

 
7/29/2007 12:08:36 PMMaGic

THESE TUTAIAL ARE GOOD GIVEN THEY HAVE BLOW-BY-BLOW ANALYSIS sorry for caps and too lazy to type again... For y'all having problems wit the programs closing quickly, add this line to the code system("PAUSE") ex. #include
using namespace std;

int main(void)
{
int myNumber;
long myNumber2 = 5;
char myCharacter;
char myCharacter2 = 'c';
bool myBoolean;
bool myBoolean2 = true;


myNumber = 3678;
myCharacter = 'a';
myBoolean = false;

cout << "myNumber = " << myNumber << endl;
cout << "myNumber2 = " << myNumber2 << endl << endl;

cout << "myCharater = " << myCharacter << endl;
cout << "myCharacter2 = " << myCharacter2 << endl << endl;
cout << "myBoolean = " << myBoolean << endl << endl;
cout << "myBoolean2 = " << myBoolean2 << endl << endl;
system("PAUSE");
return 0;
}
(If this comment was disrespectful, please report it.)

 
11/26/2007 3:27:48 AMFIRAS

Great tutorial,Jared Where is completion for these tutorials?.Really I need it.
(If this comment was disrespectful, please report it.)

 
12/17/2007 2:45:59 AMKumar Sharma

PDF tutorial files should be there so that one can send it through email.
(If this comment was disrespectful, please report it.)

 
5/12/2008 12:32:59 AMEvan

This is awsome. Not much to learn per guide but every other "beginers" guide I've checked seems to teach like you've known this for 10 years or something. 5/5
(If this comment was disrespectful, please report it.)

 
5/30/2008 1:49:33 PM

Thanks very much for this Jared. It's been really halpful for a novice myself just starting out learning C++. Atm I've been learning in Pascal so it's radically different to anything that I already know but your guides have been very helpful.
(If this comment was disrespectful, please report it.)

 
9/28/2008 3:33:48 PMxylo

thx for the tut is helfull, one small problem tho i dont xactly get it
.
(If this comment was disrespectful, please report it.)

 
1/25/2009 8:29:48 AMDavid X

In line 1, you don't declare the iostream heading ;)

#include

not

Line 1 : #include


(If this comment was disrespectful, please report it.)

 
3/29/2009 12:43:09 PMDoug

I've downloaded bloodshed & can't find a manual- Ideas? I don't want to start asking questions that might already be answered there.
112
(If this comment was disrespectful, please report it.)

 
3/29/2009 4:09:37 PMDoug

For some reason(do tell!)I kept running into a couple of errors relating to 'backward warning.h'
when I took that out of the includes in iostream.h it all started to come together!! with the added pause suggested by MaGic I think I'm well on my way....
112
(If this comment was disrespectful, please report it.)

 
5/28/2009 1:17:07 PM-newby-

awesume tutorial but u forgot in the copy part
(If this comment was disrespectful, please report it.)

 
6/2/2009 10:30:28 PM

I am having trouble. when i compile it it always comes up with something like:
`cout' undeclared (first use this function)
how do I fix that?
(If this comment was disrespectful, please report it.)

 
8/2/2009 5:42:40 PMImran

for the people having the cout problem put on the first line after the #include eg. #include
(If this comment was disrespectful, please report it.)

 
8/2/2009 5:44:02 PMImran

for people having the cout problem simply put in after the #include add in eg. #include
(If this comment was disrespectful, please report it.)

 
12/18/2009 6:28:32 AMankur madharia

y is <
(If this comment was disrespectful, please report it.)

 
3/31/2011 9:31:19 PMRandall Cook

HELP!!!!!
I am 15, I am learning how to write in c++, I have a Windows 7 OS, I have compiled the File and i have tried to open it and the most I can do is get this black box to open and close rapidly. Please Help. Thanks
(If this comment was disrespectful, please report it.)

 
10/9/2011 1:51:38 AMchingkay

i like it :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.