DIR Return Create A Forum - Home
---------------------------------------------------------
The New DS computer forum!!
HTML https://dscomp.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: C++
*****************************************************
#Post#: 286--------------------------------------------------
Tutorial #3 - Extending the Hello World! script.
By: Hondaman Date: October 9, 2010, 4:31 am
---------------------------------------------------------
[center]In this tutorial you will learn how to make your script
better
By: Adding comments and putting in more lines of text[/center]
[font=trebuchet ms]The script -[/font]
Right now you should have a script that looks something like
this
[code]
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
system ("pause");
return 0;
}
[/code]
That outputs -
[code]
Hello World!
[/code]
[font=trebuchet ms]Adding Comments -[/font]
To make your script understandable for other people,
you may want to add comments for your script to tell other
programmers what they are looking at, or how they can edit it.
For example if you had a long script you would want it to be
easy for programmers to find certain bits of code -
[code]
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int userIn = 0;
using namespace std;
cout << "\tWelcome to the Game Box V1.0" << endl;
cout << "" << endl;
cout << "Press the number you want then press" << endl;
cout << "enter to select your option please. \n" << endl;
cout << "press '0' to exit" << endl;
cout << "press '1' to play: Guess My Number" << endl;
cout << "\n" << endl;
cin >> userIn;
if(userIn == 0)
{
cout << "Bye then... " << endl;
}
if(userIn == 1)
{
srand(time(0));
int theNumber = rand() % 100 + 1;
int tries = 0, guess;
cout << "\tWelcome to Guess My Number\n\n" <<
endl;
do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;
if(guess > theNumber)
cout << "Too High! Try again...
\n\n";
if(guess < theNumber)
cout << "Too Low! Try again...
\n\n";
} while (guess != theNumber);
cout << "\nThats it! You got it in "
<< tries << " guesses!\n" << endl;
return 0;
}
system ("pause");
return 0;
}
[/code]
(This is a piece of code for my game box program(feel free to
use it, as its from my older version of thegamebox that had
nothing but the basics in it))
You may not understand any of this but, im sure that you would
like to know whats where.
This is why comments are handy, it saves time and you looking
through a gibberish of code for an error or bug.
To add comments simply put two forward slashes and anything
after that on that line is a comment.
e.g.
[code]
// Program Head
#include <iostream>
using namespace std;
// Program Body
int main()
{
// Text output
cout << "Hello World!" << endl;
// Finishing up
system ("pause");
return 0;
}
[/code]
Now you can see where everything is,
Comments are not displayed in the finished product itself.
The program ignores anything after '//' on that line
Another thing Comments come in handy for is Credits at the top
of your code.
This will take up several line though...
and it would look untidy with -
[code]
// Hello World Script
// By Hondaman
// Date Created: 09 Oct 2010
[/code]
So you want a comment block that expands across several lines.
To do this simple put '/*' your comments and at the end '*/'
e.g. -
[code]
/* Hello World Script
By Hondaman
Date Created: 09 Oct 2010 */
[/code]
And now you have credits to show who made it, what it is and
when it was created.
NOTE: Comments are shown as green code so they are easy to find.
This means you could comment out a line or block of code with
'//' or '/* */'
e.g.
[code]
cout << "Hello World!" << endl;
// cout << "How are you today?" << endl;
[/code]
and then remove the '//' or '/* */' when you need the
program to run that code.
[font=trebuchet ms]Adding more lines of text -[/font]
This has been a long tutorial so I'm going to make this quick.
Simply copy you 'cout << "Hello World!" << endl;' and paste it
on the next line,
then edit the "Hello World!" to whatever you want and that is
what it will output.
Thanks for reading this tutorial.
If you see any mistakes in my tutorials let me know by PM
(Private Message)
Hondaman.
[center]<< Previous
HTML http://www.dscompforums.tk/c/tutorial-2-hello-world!/
| Next
>>
HTML http://www.dscompforums.tk/c/tutorial-4-intro-to-variables/[/center]
*****************************************************