Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Theforeshadower's C++ Tutorials...Lesson 2: Variables  (Read 1754 times)

0 Members and 1 Guest are viewing this topic.
Theforeshadower's C++ Tutorials...Lesson 2: Vari...
« on: October 16, 2007, 09:35:26 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Welcome again to some more fun coding experiences.

Well, last week we learned how to print stuff to the console using an action such as:
Code: [Select]
cout << "Hello" << endl;We also learned how to do some math using our cout action, such as:
Code: [Select]
cout << 5 + 3 - 1 << endl;
So, what now?

Variables...or in this lesson int.

int is short for integer.

When we use it in C++, it MUST ALWAYS be lower case.  If you do not put it in lower case, your compiler will give you an error.  int commands also go inside the curly brackets.
So, what can we do with
Code: [Select]
int, you ask?
We store variables/numbers.

We use a very simple coding structure to declare integers in c++:
Code: [Select]
{
int number;
}
So, we declared the int, number.  You can use other words and numbers, but you cannot declare an int with a name starting with a number.  It can have letters and numbers, but letters must come first.

So, we declared a variable.  What now?  Well, let's use our nifty cout action to display it on the console.
We would do something like this program:
Code: [Select]
#include <iostream>
#include <stdlib.h>
int main( int argc. char *argv[])
{
     int number;  // we are declaring the int
     cout << number << endl;  // we are using cout to display the value of number
     system("Pause");
     return 0;
}
DO NOT RUN THAT PROGRAM JUST YET!

So, we declared our variable, number, with the int command.  We then used our cout action to print the value of number to the console.  But, I did not use double quotes.  That is because, since you declared number as an int(integer/variable), the system knows that and will display what number equals much like our math we did last lesson.
Now, you are also wondering what the // are all about.  That is just a way to comment your code.  You can comment your code in C++ by using //.  The compiler sees it is a comment and does not use it in the program.  This can be helpful to keep your programs neat and clean and understandable for others.  No one on a team of coders wants to handle someone else's messing code.  You can help keep it clean by simply adding comment to your code.

Well, moving on.
You see I told you not to run that program.  I hope you did not.  You see, I declared the variable,but I never set it equal to something.  If you run that, you never know what happens.  Sometimes you would get a weird random number.  That's because int is storing a variable at a point in your memory/ram.
If you do not have a value for it, C++ simply uses what was there earlier before int took over.  I ran a program like that once, I forgot to set my variable equal to a value and it crashed my computer.  Remember to always set your variables equal to a value.

Now, int variables can only be equal to numbers.  So, let's set our int to a value of 24(lol, my highschool jersey number for basketball).  We would use the following commands:
Code: [Select]
{
  int number;
  number = 24;  // we declare that the int number is equal to 24
}

So we did a simple math equation so to speak and set number equal to 24.
CAUTION:Whenever you set something equal to something else, whatever is on the left side is being set equal whatever is to the right!
What now?
Let's use our handy cout action once again.  Insert the following code in the curly brackets of your program*I am using curly brackets here, do not add them since you already have them*:
Code: [Select]
{
   int number;  // we declare the int, number
   number = 24;  // we are declaring that number = 24
   cout << "Theforeshadower's jersey number in highschool was " << number << endl;
   system("PAUSE");
   return 0;
}
I know you already have questions about what I did with cout.
Using cout, I printed onto the console "Theforeshadower's jersey...", but I continued with another set of <<.  That's because I do not want to have to make another cout action.  You can use the shortcut and after the first print in cout, add a space then a set of << and you can print some more things to the console.

If you put in the code properly and compiled and ran it, you should have this:
Theforeshadower's jersey number in highschool was 24
Press any key to continue...


So, now you know how to declare an int and how to give it a value.  You also got a bonus of how to comment your code.

Practice using the int command.  Have multiple int commands in a simple program using cout.
Later, we will begin using some more wonderful math with these int.
Until then.

EDIT:
I forgot something pretty neat you can do with declaring an int.
Instead of making a new line of code to set number = 24, you can do it like this:
Code: [Select]
{
   int number = 24;
}

C++ allows you to set your variable to a value in this line because number is already set as an int before you set it equal to 24.  Makes coding a little shorter if you are dealing with multiple variables.

Also, you can declare more than one int in a line of code.  Instead of this:
Code: [Select]
{
   int number;
   int number2;
   int number3;
}

We can do this:
Code: [Select]
{
   int number, number2, number3;
}

That would be the same as what I typed before, just shorter and in one line..It is not necessary, but keeps your code cleaner I guess and shorter.  You can also set them equal in the same line:
Code: [Select]
{
   int number = 24, number2 = 1, number3 = 15;
   cout << number << number2 << number3 << endl;
You can declare the value of the integers in the same line.  And I just used cout to shoe you that you can also print multiple variables in one line of code.  Now, if you ran a program with the above, there would be no spaces.  You would have to have a << " " << between each or the numbers would be altogether.

END EDIT.


Have fun coding!
~Theforeshadower

PS  Fell free to send me your programs via email
theforeshadower@gmail.com
« Last Edit: October 17, 2007, 01:33:53 am by Theforeshadower »
Logged
  • Super Fan Gamers!
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #1 on: October 17, 2007, 01:17:59 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 266
It's cool that you're doing this; I know what it's like to start out with this, all the help you can get is much appreciated. And for those who start here and look for other tutorials, don't be surprised that things are written differently, focus on the concepts and not the code; Everyone codes differently, every program can be written a thousand different ways.
Logged
Intel P4 3.2 GHZ
2.5GB SDRAM DDR400
350GB SATA
ATI RADEON HD 2600PRO 512MB
Creative Sound Blaster Audigy2 Z
Windows XP SP2

Current Projects: None.
- Trask
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #2 on: October 17, 2007, 01:34:38 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Extended/Updated
See the first post.
Logged
  • Super Fan Gamers!
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #3 on: October 17, 2007, 03:16:21 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
it might be worth pointing out you can only use whole numbers and also that there is a limit on the range of numbers.

Just wondering, do know how cout can tell the difference between a integer and a string?
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #4 on: October 17, 2007, 08:41:59 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 266
it might be worth pointing out you can only use whole numbers and also that there is a limit on the range of numbers.

Just wondering, do know how cout can tell the difference between a integer and a string?

a Cout doesn't know... the variable type determines all of that. Couts print out what's in quotes, then anything else is determined by the language itself. If it's something not declared or just plain wrong, it'll yell at you when you debug.
Logged
Intel P4 3.2 GHZ
2.5GB SDRAM DDR400
350GB SATA
ATI RADEON HD 2600PRO 512MB
Creative Sound Blaster Audigy2 Z
Windows XP SP2

Current Projects: None.
- Trask
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #5 on: October 17, 2007, 09:06:10 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
it might be worth pointing out you can only use whole numbers and also that there is a limit on the range of numbers.

Just wondering, do know how cout can tell the difference between a integer and a string?

a Cout doesn't know... the variable type determines all of that. Couts print out what's in quotes, then anything else is determined by the language itself. If it's something not declared or just plain wrong, it'll yell at you when you debug.
er... What are you talking about? Cout is just a function with some operator overloads, its nothing todo with the langauge standard.

And to answer your question WWFan, I think it takes void* values and then casts / formats them depending on their data type. (Don't quote me on that though, not 100% sure)
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #6 on: October 18, 2007, 12:04:55 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 266
It's a basic function, that's what I meant; it's standard.
Logged
Intel P4 3.2 GHZ
2.5GB SDRAM DDR400
350GB SATA
ATI RADEON HD 2600PRO 512MB
Creative Sound Blaster Audigy2 Z
Windows XP SP2

Current Projects: None.
- Trask
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #7 on: October 19, 2007, 06:54:28 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
And to answer your question WWFan, I think it takes void* values and then casts / formats them depending on their data type. (Don't quote me on that though, not 100% sure)
(Sorry for quoting you but) Void pointers, do they exist -.-? I mean, how can a pointer point to nothing...? Unless "void" was a virtual class and "int", "short", etc. were derivated classes, but that's not the case, I suppose... hmm... I think "cout" uses templates instead, they could be more easily changed into strings. I don't know if this conversion is automatic though; I guess that function uses decimal logarithms, module, and possibly bitwise operators to make the conversion with efficience.
« Last Edit: October 19, 2007, 06:57:12 pm by sjegtp »
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #8 on: October 20, 2007, 04:43:33 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
And to answer your question WWFan, I think it takes void* values and then casts / formats them depending on their data type. (Don't quote me on that though, not 100% sure)
(Sorry for quoting you but) Void pointers, do they exist -.-? I mean, how can a pointer point to nothing...? Unless "void" was a virtual class and "int", "short", etc. were derivated classes, but that's not the case, I suppose... hmm... I think "cout" uses templates instead, they could be more easily changed into strings. I don't know if this conversion is automatic though; I guess that function uses decimal logarithms, module, and possibly bitwise operators to make the conversion with efficience.
Yes they do, void pointers are basically just pointers that can point to any memory location irrespective of data type. In this case the word void is just used to mean 'undefined'.

Try it out for yourself.
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 2: ...
« Reply #9 on: October 20, 2007, 06:43:23 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Try it out for yourself.
*Tries out* :D cool! Now I don't need to use templates anymore. n__n
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.041 seconds with 57 queries.