Author Topic: Day 1 of me learning C++  (Read 11671 times)

0 Members and 2 Guests are viewing this topic.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Day 1 of me learning C++
« on: June 21, 2013, 04:19:58 pm »
Hello guys, I started on C++ last night, here is what I wrote and put all my notes so I remember and know what each line is for.


Let me know if my notes are accurate, or if there are any mistakes or if anything I should just know! Thank you!


Code: (cpp) [Select]
//Test one using C++ by Chef

//Lines beginning w/ # are directives for the preprocessor.
#include <iostream>
// Tells the preprocessor to include iostream standard file.
// iostream includes declarations of basic input-output library
using namespace std;
//namespace std(standard) is the entity in which we are writing code.
int main ()
// beginning of the main function, start of execution, all C++
// programs must have a main function, makes this function #1
{
   //cout functions requires iostream. This is a statement line.
   cout<<"Hello World!";
   //semicolon is the end of a statement in C++.
   //Most common syntax error is not using semicolon.
   return 0;
   //return statement causes main function to finish.
}
//cout is the standard output stream in C++.
//cout is always declared in the iostream standard file within the
//std namespace.






//The code above has the same functions as the one below, just written differently.
#include <iostream> using namespace std; int main () {cout<<"Hello People!"; cout <<"Testing 1 2 3!"; return 0;}
« Last Edit: June 21, 2013, 05:02:13 pm by ande »
"To find happiness is to not always laugh."

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Day 1 of me learning C++
« Reply #1 on: June 21, 2013, 04:23:18 pm »
Code can be posted in a  [ code=Cpp] tag (remove the space).

Offline dense

  • Serf
  • *
  • Posts: 23
  • Cookies: 4
    • View Profile
Re: Day 1 of me learning C++
« Reply #2 on: June 21, 2013, 04:33:48 pm »
The last line is incorrect, preprocessor directives need a line for themselves. Other than that, if you actually understand each line of the code then you're at a great start  ;)

Offline Phage

  • VIP
  • Overlord
  • *
  • Posts: 1280
  • Cookies: 120
    • View Profile
Re: Day 1 of me learning C++
« Reply #3 on: June 21, 2013, 04:34:28 pm »
I don't hope you are going to use Evilzone as a diary and post your progress everyday?

- lol, you have made a Hello, World! in C++ there's not much to explain about that. Otherwise what dense wrote.
"Ruby devs do, in fact, get all the girls. No girl wants a python, but EVERY girl wants rubies" - connection

"It always takes longer than you expect, even when you take into account Hofstadter’s Law."

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #4 on: June 21, 2013, 04:49:25 pm »
No I'm not going to use EZ as a diary. I just want to, since its my start, ask y'all make sure I'm doing it correctly. I'll only have questions as I go along.
 
Could you all explain what line is incorrect? I thought #include <iostream> was the preprocessor directive?
"To find happiness is to not always laugh."

Offline dense

  • Serf
  • *
  • Posts: 23
  • Cookies: 4
    • View Profile
Re: Day 1 of me learning C++
« Reply #5 on: June 21, 2013, 04:53:43 pm »
The last one, #include <iostream> has to have it's own line.
Code: [Select]
#include <iostream>
using namespace std; int main () {cout<<"Hello People!"; cout <<"Testing 1 2 3!"; return 0;}

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #6 on: June 21, 2013, 04:59:15 pm »
The last one, #include <iostream> has to have it's own line.
Code: [Select]
#include <iostream>
using namespace std; int main () {cout<<"Hello People!"; cout <<"Testing 1 2 3!"; return 0;}

Oh okay, I understand now. It didn't say anything about that in the tutorial! I was actually written the same way.

Now I know! ;)
"To find happiness is to not always laugh."

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Day 1 of me learning C++
« Reply #7 on: June 21, 2013, 05:03:40 pm »
Code can be posted in a  [ code=Cpp] tag (remove the space).

Fixed OP's post.


I don't hope you are going to use Evilzone as a diary and post your progress everyday?

- lol, you have made a Hello, World! in C++ there's not much to explain about that. Otherwise what dense wrote.

Why not? I think its great. Probably useful for others learning C++.


No I'm not going to use EZ as a diary. I just want to, since its my start, ask y'all make sure I'm doing it correctly. I'll only have questions as I go along.

Use EZ as your diary if you like. Maybe not EVERY day tho. Depends how much work you put into each day I guess. :)
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #8 on: June 21, 2013, 05:06:06 pm »
Fixed OP's post.


Why not? I think its great. Probably useful for others learning C++.


Use EZ as your diary if you like. Maybe not EVERY day tho. Depends how much work you put into each day I guess. :)

Pretty much what I was gonna do. lol




I'm pretty damn hooked on this C++ right now. I wish I was at home, I downloaded Microsoft Visual Studios last night which is pretty awesome. 30 day trial though. :(  Any others out there I could use in replace of MS?I haven't downloaded a compiler either yet!




Staff note: Please dont double post.
« Last Edit: June 21, 2013, 05:16:41 pm by ande »
"To find happiness is to not always laugh."

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Day 1 of me learning C++
« Reply #9 on: June 21, 2013, 05:18:47 pm »
Pretty much what I was gonna do. lol


I'm pretty damn hooked on this C++ right now. I wish I was at home, I downloaded Microsoft Visual Studios last night which is pretty awesome. 30 day trial though. :(  Any others out there I could use in replace of MS?I haven't downloaded a compiler either yet!


Staff note: Please dont double post.


You can download the express edition of visual C++, no limitations there. It is of course not as massive as the entire visual studio package but it gets the job done. Alternatively you can use something like code::blocks, Dev-C++ or netbeans c++ package.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #10 on: June 21, 2013, 05:22:47 pm »

The global variables are the variables you "declared to be those variables", right?
And awesome, I will download that tonight!
 
And what? Did I double post?
« Last Edit: June 21, 2013, 05:24:25 pm by Chef »
"To find happiness is to not always laugh."

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Day 1 of me learning C++
« Reply #11 on: June 21, 2013, 05:33:49 pm »

The global variables are the variables you "declared to be those variables", right?
And awesome, I will download that tonight!
 
And what? Did I double post?

You decide what variables are global.

Yeah, you posted two posts after each other. No biggie.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Neea

  • Peasant
  • *
  • Posts: 83
  • Cookies: 19
  • Laboris Gloria Ludi
    • View Profile
Re: Day 1 of me learning C++
« Reply #12 on: June 21, 2013, 06:21:51 pm »
My advice is, don't abuse of global variables.... sure as you learn they're easier to just declare them, but most of them can be replaced with local variables. Also you definitely need to learn the difference between them. Global variables exist thorough the execution of your program, meaning if you modify their value in a function, that value will persist as you return in the main body (also you will learn all about functions soon), whereas if you have a local variable in a function it's value will no longer exist after that function finishes its execution ..... unless you explicitly return it.


Example:



Code: (Cpp) [Select]
#include <iostream>
using namespace std;


int globalVariable;


void myFunction(){
    int localVariable=34;
    globalVariable++; //or globalVariable = globalVariable+1;
}


int myFunct(){
    int var = 34;
    return var;
}
int main(){
    globalVariable = 0;
    cout << "Global variable = " << globalVariable << endl;  // will print 0
    myFunction();
    cout << "Global variable = " <<globalVariable << endl;  //will print 1
    cout << " Local variable = " << localVariable << endl; //you will get a warning of undifined variable, will print out trash that is at that memory address
    int localVar = myFunct();
    cout << "LocalVar = " << localVar << endl; // will print 34


    return 0;


}
<!--Here be Cthulhu -->

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: Day 1 of me learning C++
« Reply #13 on: June 21, 2013, 06:24:36 pm »

Okay so I just got introduced to strings from the tutorial.


Code: (cpp) [Select]
//strings can be written w/ out an initial value but given one
// during execution.
#include <iostream>
#include <strings>
using namespace std;
int main ()
{
   string mystring;
   mystring = "This is my no value string.";
   cout << mystring <<endl;
   mystring = "My string now has a value.";
   cout << mystring << endl;
   return 0;
}


When starting a string that has no value but gets a value during execution why does it start w/ "string mystring;"?


Why does the string end w/ "cout << mystring << endl;"? What is "endl;"?


Thanks guys.

Staff Note: Code tags bro lol.
« Last Edit: June 22, 2013, 06:21:31 am by techb »
"To find happiness is to not always laugh."

Offline theifyppl

  • Serf
  • *
  • Posts: 44
  • Cookies: 20
    • View Profile
Re: Day 1 of me learning C++
« Reply #14 on: June 22, 2013, 06:02:58 am »
Okay so I just got introduced to strings from the tutorial.



//strings can be written w/ out an initial value but given one
// during execution.
#include <iostream>
#include <strings>
using namespace std;
int main ()
{
   string mystring;
   mystring = "This is my no value string.";
   cout << mystring <<endl;
   mystring = "My string now has a value.";
   cout << mystring << endl;
   return 0;
}


When starting a string that has no value but gets a value during execution why does it start w/ "string mystring;"?


Why does the string end w/ "cout << mystring << endl;"? What is "endl;"?


Thanks guys.


The "string mystring;" line is declaring a string variable named "mystring."  The next line is assigning a value to that string.  It's not mandatory that you do it that way.  You could simply just do it all in one line:


Code: (cpp) [Select]
string mystring = "This is my string";

As for your second question, "endl;" tells the standard output to end the line here.  So, for example, if you had:


Code: (cpp) [Select]
cout << "Hello" << endl << "World";

The output would be:


Hello
World
« Last Edit: June 22, 2013, 06:10:28 am by theifyppl »