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

Pages: [1] 2   Go Down

Author Topic: Theforeshadower's C++ Tutorials...Lesson 1  (Read 3800 times)

0 Members and 1 Guest are viewing this topic.
Theforeshadower's C++ Tutorials...Lesson 1
« on: October 11, 2007, 10:55:02 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Before I begin, here are some things I hope you have figured out:
-you have a C++ editor and compiler(get Dev C++ if you do not have one www.bloodshed.net )
-you know where to type your code
-you know the basics of using your compiler and editor
-I am using Dev C++ 5 Beta 4.9.9.2 so I will not be familiar with questions pertaining to Visual C++ or other editors
-you know you are free to ask any questions

With that said, let's begin!
First, open Dev C++.  Select new project.  Pick the Console option.  You should see your editing area changed with a few lines of code.  That's normal.  Let's start with a basic action-Displaying words in the console.

In order to have the console print words and numbers, you have to use the cout action.
An example of this would be: cout << "I have begun my journey into C++" << endl;

Pretty basic, but where would that go?
Well, you'll notice that your program already has several codes already.  Do you see the int main(int argc, char *argv[]) line?  Good.  Just under that,you will see the swirly bracket or {.  That is the beginning of your program.  If you want your program to do anything, it must be after that bracket but before the closing } at the end.

So lets try this:(Your program should look like this...Also, whatever I put"[b and /b]" around is what you need to type in)
Code: [Select]
#include <iostream>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     [b]cout << "I am beginning my journey into C++" << endl;[/b]
     system("PAUSE");
     return 0;

}
Let's break that down:
You typed in the cout action.  Notice that it is lower case.  C++ is case sensitive.  cout must be lower case.  So we have a cout action telling the console to print "I am beginning my journey into C++".  Whenh you want cout to just print words, they must be enclosed with double quotes.  If they are not, then you compiler will be looking for strings or ints or other things that match whatever you typed.
We ended our cout with another set of << but added endl after them.  That just tells the program to start at a new line.  Some also use the \n to make a new line.  Such as cout << "Hello \n";  It is just a shorter way of moving to the next line.
We end the whole line of code with a semicolon or ;.  You MUST MUST do this for just about every code.  If you do not, then your compiler will give you an error.  If just shows the program that that is the end of you line of code.

So, what are you waiting for?  Goto Compile and Run and see what happens...
You should see a ms-dos window open with the words "I am beginning my journey into C++"
It also says press any key to continue.  You do so and the program closes...Why is that?

Well, back in your code, you have the line:
system("PAUSE")
That is basically forcing the program to stop and wait for you to push any key to continue with the program.
Last, you have the return 0 code.
That just tells the program to return a value of 0 which ends the program.

Mess around with the cout action a bit until you are comfortable with it.  Try typing more than one command of it and see what happens.  Until next week, have fun with coding!
~Theforeshadower
Logged
  • Super Fan Gamers!
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #1 on: October 12, 2007, 12:52:50 pm »
  • The devil in the mirror.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 238
Nice tutorial, I already knew C++ tho, so I knew it all ^^

Anyho:
1. This is just my opinion, but when looking at the code, take one line at the time, instead of explaining it all in one big chunk of text, like:

"
Code: [Select]
cout << "I am beginning my journey into C++" << endl;cout prints text to the current output, in this case, the command screen.
The text is kept within "" and you can add more than one element by adding more arrows <<.
endl, witch is kept inside the namespace std, means that it will start at a new line after that, and ending with endl is often a good idea.
"

Just my opinion tho because I like reading new stuff like that ;)


2. You forgot the "using namespace std;"

or else you have to put std:: in front of both cout and endl;
« Last Edit: October 12, 2007, 12:55:23 pm by wildex999 »
Logged
Working on my Masters Degree in Computer Science.
Hey look, Zelda Coop....later.
Proud user of C++ for 9 years, and counting!

Silvanesti

Re: Theforeshadower\'s C++ Tutorials...Lesson 1
« Reply #2 on: October 12, 2007, 01:10:57 pm »
Awesome! I\'m trying to learn C++ and it\'s rather hard just from sites. I\'ll use these tutorials alot.
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #3 on: October 12, 2007, 01:13:09 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6206
*Cough* Hello World Example *Cough*
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #4 on: October 12, 2007, 03:37:30 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
*Cough* Hello World Example *Cough*

yes, that would be the purpose of this, as is the purpose of most beginner programming tutorials =\
Logged



i love big weenies and i cannot lie
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #5 on: October 12, 2007, 05:31:22 pm »
  • Huzzah!!!
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 374
Cool. Thanks for starting a c++ tut.

I was thinking, maybe at the end of each post you could include an index of everything you've included with a little description:

i.e.

Cout - Cout does blah blah
       - by doing this you can print text on the screen (link to the post that more important info about this is on)
endl - does blah (link)


This will make it really easy for people to quickly find things that they have forgotten or need to brush up on.



UPDATE: I did all the stuff in the 1st tut. It all worked. I am happy. BUT I want more!!!!
« Last Edit: October 12, 2007, 05:52:18 pm by Red_Penguin »
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #6 on: October 12, 2007, 06:05:44 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
You forgot either the "std::" or the using "namespace std;"

Most beginners start with the latter :P
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #7 on: October 12, 2007, 06:17:03 pm »
  • The devil in the mirror.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 238
You forgot either the "std::" or the using "namespace std;"

In his example code yes(As I also said in my post)
But, if they do as he say, and start a new project, it usually is there in the first file, and therefore will work as long as they don't Copy&Paste

But you can almost call this more of a example than a tutorial =/ Its a wee bit short...
Logged
Working on my Masters Degree in Computer Science.
Hey look, Zelda Coop....later.
Proud user of C++ for 9 years, and counting!
Re: Theforeshadower's C++ Tutorials...Lesson 1.5...
« Reply #8 on: October 12, 2007, 10:14:18 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Thanks for the comments and criticism.
Continuing from Lesson 1...

More fun with cout.

Did you know that C++ can handle mathematics?  Well, it does.
We are going to show this using our cout action.

First, what are the symbols for math in C++?

+    Addition
-    Subtraction
*    Multiplication
/     Division

So, now we know the symbols.  How would we use them with cout?
Very simple really.  Let's just use our program from before...

Code: [Select]
#include <iostream>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     cout << "I am beginning my journey into C++" << endl;
     system("PAUSE");
     return 0;

}

There is our code from before.  Cout is printing "I am beginning my journey into C++".
System("PAUSE") is halting the program until we push any key.  Return 0 ends the program.

We understand that part.
Let's get to some math fun!

Using our cout action, we can use a line like this:
Code: [Select]
cout << "1+1 = 2" << endl;You are using cout to print "1+1 = 2"...But, you already knew that.  All you did was print out that 1 + 1 = 2, nothing more.  How would we use cout for math?
Like this:
Code: [Select]
cout << 2 + 3 << endl;
Now, you may be thinking,"Wait!  We did not use quotation marks!"
It's okay.  C++ understands what 2+3 is.  Cout can be used to print the answer.  Instead of printing a phrase or group of words, we are printing out math problems.  Cout takes the answer of 2 + 3 and prints it on the screen, which would be 5.

So you think that is easy and everyone should know how to do that?
Well, try this:
Code: [Select]
#include <iostream>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     cout << "I am beginning my journey into C++" << endl;
     [b]cout << 2 + 3 * 2 / 2 << endl;[/b]
     system("PAUSE");
     return 0;

}

Compile and Run.  You see the number "5" is printed onto the console.
You say, "Well,duh, 2 + 3 is 5. 5 times 2 is 10.  10 divided by 2 is 5."
You are correct but not right.  You see, C++ works out equations and math just like algebra.
C++ sees our code as this-
cout << 2 + [( 3 * 2 ) / 2] << endl;
Why is that?

Because, in algebra, math is done in the following sequence-
Parenthesis
Exponents
Multiplication
Division
Addition
Subtraction

or simply PEMDAS.

Multiplication and Division are equal.
Addition and subtraction are equal.

So why would it do multiplication first over division?  Because our multiplication came before our division in our code.  If we put parenthesis around ( 2 / 2 ), it would have calculated that, then the multiplication.
Our answer is still five, of course because- 2 + 3 * (2 / 2) is as such:
First is parenthesis, 2 / 2, which equals 1.
Then 1 is multiplied by 3 which equals 3
And lastly, 3 is added with a 2 which equals 5.

I hope this is not too troubling.

Experiment using the cout to do some math.

REMEMBER THIS: For now, only do math that gives you whole numbers for answers, not fractions or decimal.  You will not get the full correct answer.  We will discuss this further into the lessons.

Have fun coding!
~Theforeshadower
Logged
  • Super Fan Gamers!

Silvanesti

Re: Theforeshadower\\\'s C++ Tutorials...Lesson ...
« Reply #9 on: October 12, 2007, 10:25:22 pm »
I don\\\'t know about you. But I always learned:

Brackets
Exponents
Division
Multiplication
Addition
SSubtraction
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #10 on: October 12, 2007, 10:28:18 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
That is essentially the same as what I learned.  Where did you take Algebra?  I do not know if English(American) Algebra is taught the same as in other countries.  We always used Parenthesis.  We also used brackets but that is in the case of having 2 or more equations in prenthesis we wanted calculated together.  I am not sure if C++ uses brackets.  I know for a fact it follows the PEMDAS system.
Logged
  • Super Fan Gamers!
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #11 on: October 12, 2007, 11:05:38 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
That is essentially the same as what I learned.  Where did you take Algebra?  I do not know if English(American) Algebra is taught the same as in other countries.  We always used Parenthesis.  We also used brackets but that is in the case of having 2 or more equations in prenthesis we wanted calculated together.  I am not sure if C++ uses brackets.  I know for a fact it follows the PEMDAS system.
In C++ only parenthesis. Like this:
x = 2 + 3 * (y + sqrt(k - 2 + abs(sin(3.141592655))));

(EDIT:)
In my country, mathematics is a little different from the U.S.... for instance:
Decimals are written with commas, and big numbers use dots. Like this:
pi = 3,141592655
c = 300.000.000 m/s
We also use dots for multiplying numbers, for instance, "y = 2 x 4" (2 times 4) is the same of "y = 2 . 4"
And equations are written this way: "x = 2 + {9 . 32 . [h + (9 + y)(23 - k)]}" but usually people don't use this convention and simply write "x = 2 + (9 . 32 . (h + (9 + y)(23 - k)))"



Anyway, it's seems that your C++ studies have been going quite well, TFS :D
What've you learned so far?
« Last Edit: October 12, 2007, 11:16:07 pm by sjegtp »
Logged

Silvanesti

Re: Theforeshadower\'s C++ Tutorials...Lesson 1
« Reply #12 on: October 13, 2007, 12:17:26 am »
That is essentially the same as what I learned.  Where did you take Algebra?  I do not know if English(American) Algebra is taught the same as in other countries.  We always used Parenthesis.  We also used brackets but that is in the case of having 2 or more equations in prenthesis we wanted calculated together.  I am not sure if C++ uses brackets.  I know for a fact it follows the PEMDAS system.
Well if C++ does use the PEMDAS system I may as well use it. :P
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #13 on: October 13, 2007, 09:13:28 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
I was taught BODMAS here in England :P

Brackets
Other
Division
Multiplication
Addition
Subtraction

Other being things like ^2
Logged

Silvanesti

Re: Theforeshadower\'s C++ Tutorials...Lesson 1
« Reply #14 on: October 13, 2007, 06:08:34 pm »
I was taught BODMAS here in England :P

Brackets
Other
Division
Multiplication
Addition
Subtraction

Other being things like ^2
^2 is an exponent, so I don\'t understand why it would be Other instead of Exponents. But oh well, I guess it\'s different in England.
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #15 on: October 13, 2007, 07:03:59 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
We were never told that word, guess they thought we wouldn't understand. Shame, because I would easily, some of the others in my class probably wouldn't, but I always say Screw Them anyway.

You wouldn't believe some of the idiots that make it into top set science at GCSE...most of them are failing it...me, I usually only get one or two questions wrong in every test. 79/84 in Module 7 (ironically 5 marks of that were for Compass questions, I suck at using a compass big time), 93/94 in Modult 8, and we haven't done Module 9 yet (or Module 10, but that's only for people predicted to get A*'s like me :D).
Logged

Silvanesti

Re: Theforeshadower\'s C++ Tutorials...Lesson 1
« Reply #16 on: October 13, 2007, 08:21:15 pm »
We were never told that word, guess they thought we wouldn\'t understand. Shame, because I would easily, some of the others in my class probably wouldn\'t, but I always say Screw Them anyway.

You wouldn\'t believe some of the idiots that make it into top set science at GCSE...most of them are failing it...me, I usually only get one or two questions wrong in every test. 79/84 in Module 7 (ironically 5 marks of that were for Compass questions, I suck at using a compass big time), 93/94 in Modult 8, and we haven\'t done Module 9 yet (or Module 10, but that\'s only for people predicted to get A*\'s like me :D).
Yah, I don\'t know about you but where I live schooling is HARD. The stuff im doing now in grade 10 is what my parents were doing in grade 12. I suppose my University/College will be alot harder. I don\'t understand why they think \"exponents\" is such a hard word to learn. The rules are pretty basic.
Logged
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #17 on: October 13, 2007, 08:27:52 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
I was taught BODMAS here in England :P

Brackets
Other
Division
Multiplication
Addition
Subtraction

Other being things like ^2

same here (from england also)
but you sure that "O" wasn't "of" from "power of" (5 to the power of 3 = 5^3)
(and it being O mean't it could be pronounced as a whole word. bodmas, instead of bpdmas)

but either way i can barely remember lol
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World
Re: Theforeshadower's C++ Tutorials...Lesson 1
« Reply #18 on: October 13, 2007, 08:41:51 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Yah, I don\'t know about you but where I live schooling is HARD. The stuff im doing now in grade 10 is what my parents were doing in grade 12. I suppose my University/College will be alot harder. I don\'t understand why they think \"exponents\" is such a hard word to learn. The rules are pretty basic.
Wait, you mean subject, not word, right?
Anyway...
"Exponents", hard to learn? lol.
I wonder what they think about logarithms, Stoichiometry, Enantiomery...
Logged

Silvanesti

Re: Theforeshadower\'s C++ Tutorials...Lesson 1
« Reply #19 on: October 13, 2007, 08:45:47 pm »
Yah, I don\\\'t know about you but where I live schooling is HARD. The stuff im doing now in grade 10 is what my parents were doing in grade 12. I suppose my University/College will be alot harder. I don\\\'t understand why they think \\\"exponents\\\" is such a hard word to learn. The rules are pretty basic.
Wait, you mean subject, not word, right?
Anyway...
\"Exponents\", hard to learn? lol.
I wonder what they think about logarithms, Stoichiometry, Enantiomery...
My mistake. I\'m rather tired right now. XD

Also, I just finished both of the mini-tuts. So I understand a tiny bit about C++. I\'m excited for the next tutorial.
Logged
Pages: [1] 2   Go Up

 


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



Page created in 0.067 seconds with 74 queries.

anything