PDA

View Full Version : SonniE's C++ Tutorial



SonniE
09-25-2008, 03:11 PM
Im going to make a series of Beginners Tutorials for C++ programming since people on the forum are trying to learn it. going to add to this frequently and i will upload the project/source files so you can play with them.
1) Hello World

/*
SonniE's Hello World C++ Example 1
This Program Shows basic console printing skills
*/
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World";
system("pause");
}

2) Input Output Strings

/*
SonniE's Input Output Example 2
*/
#include <iostream>
using namespace std;
int main()
{
//Creates The String userinput
string userinput;
//Prints To Screen
cout<<"Enter Your Name: ";
//Takes What User Typed in and puts it in userinput string
cin>>userinput;
//Prints Userinput String To Screen
cout<<userinput;
system("PAUSE");
}


3) Example usage of Define

#include <iostream>
#define pausethish0e system("pause");
#define showmetext cout
using namespace std;
int main()
{
showmetext<<"This Shows u What you can do with a define" << endl;
pausethish0e
}


4) Looping with Labels

#include <iostream>
#define pausethish0e system("pause");
using namespace std;
int main()
{
thisisalabel:
cout<<"Example oF Looping with a label" << endl;
pausethish0e
goto thisisalabel;
}


5) Looping with do while

#include <iostream>
#define pausethish0e system("pause");
using namespace std;
int main()
{
do{
cout<<"Example oF Looping with a while true" << endl;
pausethish0e
}while(true);
}

6) Breaking a Do while Loop

#include <iostream>
using namespace std;
int main()
{
do{
string ans;
cout<<"Example oF Looping with a while true with a break Enter '1' to Break the loop" << endl;
cin>>ans;
if(ans=="1")
break;
}while(true);
}


7) Simple Counting up (counts up to 10)

#include <iostream>
using namespace std;
int main()
{
for(int counter=0; counter<=10; counter++)
{
cout<<counter<<endl;
}
system("pause");

}

Mako-Infused
09-25-2008, 03:14 PM
nice and simple. good guide sonnie

SonniE
09-29-2008, 03:22 AM
added 4 more tuts im going to add more later

S1N
07-07-2009, 11:41 PM
Yeah thats true but it works..

I used this to begin my first C++ a couple months back

TheEliteOne
03-03-2010, 08:15 AM
Hey can you explain what the hell is going on please? lol =]

SonniE
03-07-2010, 01:29 AM
Hey can you explain what the hell is going on please? lol =]
do you have a specific question?

TheEliteOne
03-15-2010, 09:36 AM
do you have a specific question?

Okay, I've made a Hello World app for the PSP in C following a guide before I know one or two things



/*
SonniE's Hello World C++ Example 1
This Program Shows basic console printing skills
*/
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World";
system("pause");
}


I know about // and /* */ for ignoring lines and stuff, it's this stuff that I get lossed at (This is very sad I know it =| )



#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World";
system("pause");
}


Is { used to start a function and } used to end a function?

SonniE
03-16-2010, 07:50 PM
the { } brackets enclose more than just methods (ex. main) they do everything from conditional if statements to loops.

TheEliteOne
03-19-2010, 12:30 AM
Lol



/*
SonniE's Hello World C++ Example 1
This Program Shows basic console printing skills
*/
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World";
system("pause");
}


You should have used this: (Less sloppy)



/*
SonniE's Hello World C++ Example 1
This Program Shows basic console printing skills
*/
#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World\n";
system("pause");
}


Other wise it's like this:

http://img534.imageshack.us/img534/4886/13975297.png

SonniE
04-18-2010, 09:14 PM
Lol



You should have used this: (Less sloppy)



Other wise it's like this:

http://img534.imageshack.us/img534/4886/13975297.png

it doesn't need the linebreak the purpose of the code was to show the function that prints text. But if there was more to the program i would add a linebreak. You could use \n or the endl keyword.

bad00boy
04-20-2010, 05:41 PM
is that a tutorial? O_O' lol , can u plz explain what is the ''#include <iostream>'' & "system("pause");" & "int main()" & "/ *" ? and where to add these lines?

TheEliteOne
04-30-2010, 12:50 AM
is that a tutorial? O_O' lol , can u plz explain what is the ''#include <iostream>'' & "system("pause");" & "int main()" & "/ *" ? and where to add these lines?

#include:

Includes a file from the library into the program when you compile it.

system("pause");

Performs the system call "pause" which makes the text "Press any key to continue . . . " and will make the user press any key to continue on. Used to pause the program (Hints the name)

int main()

Shows that we are in are main function.

/ *

Are for division and multiplication. + = add - = subtract * = mulitply and / = divide. (Check my Calculator example for more info on this)

;)