Create a free forum in seconds.
InvisionFree - Free Forum Hosting
Welcome to Acer's Supreme Forum Hits. One of the most, well, slick forums that we have. We hope you enjoy your visit and had a good time.


Obviously if you're reading this notice, you're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. Though only the Guests Room Related board is the ONLY ONE YOU CAN USE! If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. And you know it's freeeeeeeeee! So join and sign up now!


Join our community!


If you're already a member please log in to your account to access all of our fantastic forum features that we may have here in store for you. Thanks! And happy posting, y'all!

Name:   Password:


Click on the TopSite Button to get a hit for Acer's Supreme Forum Hits! Please do this every day if you have to! Thanks! The recommended TopSite button you should click is one it says "Official Clans Topsites". The old "Tutorial Base Topsites" has been deleted for further notice. Thanks!
Official Clans Topsites List
 

 Shotgun Session C++, Tutorial 1
Dark_Jackal
Posted: Jul 29 2005, 02:58 PM


Seldom Experienced Member


Group: Forum Members
Posts: 177
Member No.: 115
Joined: 7-June 05



In order to create GUI programs you must lean the basics. All the begining programs run in dos. So eh. you must learn dos programming before you learn GUI. Anyway this is only the 1st of many to come. If people actually read them. >.>

PART 1

Before we really get into C++ programming, here is a simple program:

/* Your First C++ program O.o

Enter this program into a complier and run it. If you want to get a "Hands on" experence with this example.
*/
#include <iostream>
using namespace std;

// main() is where the progam execution begins.
int main()
{
cout << "This is an extreamly simple program.";
return 0;
}


When and if you run it you should see this on the screen

This is an extreamly simple program.

Ok, now that you have seen that example. Let's explain what each line means.

/* Your First C++ program O.o

Enter this program into a complier and run it. If you want to get a "Hands on" experence with this example.
*/


That is a comment. Like other languages, C++ lets you add a remark into a program's source code. Please note the open and close commands...

#include <iostream>

The C++ language has several headers. They contain information that is either necessary or useful to your program. A header is included in your program by using the #include directive.

using namespace std;

This tells the complier to use the std namespace. Namespaces are a recent addition to C++. A Namespace creates a declarative region in which various program elements can be placed.

// main() is where the progam execution begins.

This is the second type of comment in the C++ language. Often called the Single line comment.

int main()

This is where the program begins. All C++ programs are created of one or more functions. Every function must have a name, and the only function that any C++ program must include what is shown here, called main(). The main() function is where the proram execution begins and (most commonly) ends.

Note: the opening and closing brakets is where the program code goes: See example below.

{
Program code here
}


cout << "This is an extreamly simple program.";


This is a console output statement. It causes the message This is an extreamly simple program to show on the screen. It does this by using the output operator <<. The << operator causes whatever expression is on its right side to be output to the device specified on its left side. cout basically applies to a computer screen.

Note: All C++ statements end with a semicolon (;). See cout example above.

return 0;

This line terminates the main(). I could get into more detail but it basically means to end the program after the functions are complete.

Now that you know the basics, lets get into another example.

Here is a second really simple program. An Example of a value to a variable.

//How to use a value to a variable

#include <iostream>
using namespace std;

int main()
{
int value; // declares a variable
value = 1023; // assigns 1023 to value

cout << "This Simple Program Prints The Value: ";
cout << value; // Displays 1023

return 0;
}


Lets look at this further:

int value; // declares a variable

This declares a variable called value of type integer. In C++ all variables must be declared before they are used and the type of values that the variable can hold must also be specified. Value can only hold intergers, the range is -32,768 to 32,767. int mean that you are declaring a variable to be of type interger. C++ supports a whole lot of data types, but you can also create your own.

value = 1203; // assigns 1023 to value

As I stated in the comment this assigns the value 1023 to value. In C++, the assignment operator is the single equal sign. After the assignment the variable value will contain the number 1023.

cout << value; // Displays 1023

cout displays the output generated by the program. Basically if you want to display the value of a variable put it on the right side of << in a cout statement.

This program displays the value 1023 on the screen.

Try giving value other values and watch the results.

PART 2
Now for a hmmm more pratical example....

//This Converts gallons to liters.

#include <iostream>
using namespace std;

int main()
{
int gallons, liters;

cout << "Enter number of gallons: ";
cin >> gallons; // this inputs from the user

liters = gallons * 4; // Convert to liters

cout << "Liters: " << liters;

return 0;
}


The program first displays a prompting message on the screen, then waits for you to enter a whole number amount of gallons.

Note: Interger types cannot have fractional componets. There are actually 3.7854 liters in a gallon, but since intergers are used in this example it is rounded.

cin >> gallons; // inputs from the user

cin is another predifined identifier that is provided with your C++ complier. Basically stands for Console input (Keyboard). The input operator is the >> symbol and blah blah blah.

cout << "Liters: " << liters;

It uses two output within the same output statement.It outputs the string "Liters: " followed by the value of liters. You can chain together as many output operators as you like within one output statement. Just use a seperate << for each item.

Now for a new data type :D

/* this program converts gallons to liters using
floating point numbers. */

#include <iostream>
using namespace std;

int main()
{
float gallons, liters;

cout << "Enter the number of gallons: ";
cin >> gallons; // inputs from the user

liters = gallons * 3.7854; // convert to liters

cout << "Liters: " << liters;

return 0;
}


I have made two changes. First off, I gallons and liters are declared as floats. This data type will typically be in the range 3.4E-38 to 3.4E+38. Don't ask me what that means I am not sure of it, but that is a limit so I thought I should site it but I have never come into any problems because of it, so eh. Basically this program provides a more accurate converson based on 3.7854 which equals one gallon. :D

Note: C++'s I/O system automatically adjusts to whatever type of data you give it.

Try that program. Enter 1 gallon when prompted. The equivalent number of liters is now 3.7854.


REVIEW

1. All C++ programs must have a main() function, and it is there that program execution begins.
2. All variables must be declared before they are used
3. C++ supports a variety of data types, including integer and floating point.
4. The output operator is <<, and when used with cout, it causes information to be displayed on the screen.
5. The input operator is >>, and when used with sin, it reads information from the keyboard.
6. Program execution stops when the end of main() is encountered (or the return 0; statement is executed).


--------------------
In meinem Herzen ist ein Hakenkreuz, das mit Stolz und Ehre gehalten wird!
-----------------------------------------------------------------------
In my heart is a swastika, which is held with pride and honour!
Top
Alex
Posted: Jul 29 2005, 03:15 PM


Advanced/Supremest Member


Group: Forum Members
Posts: 554
Member No.: 166
Joined: 7-July 05



I din't read it all... but nice tutorial!!


--------------------
user posted image
user posted image
^Both made by me!^
user posted image
^MEMBER OF THE MONTH OF JULY!!!! MY BIRTHDAY MONTH WOOHHOOOO!!!^
Top
Solid Snake
Posted: Aug 28 2005, 04:10 PM


General Newbie Member


Group: Forum Members
Posts: 24
Member No.: 232
Joined: 28-August 05



Well you should you could learn something even it is not your kind of thing you like


--------------------
Top
hen5522
Posted: May 1 2012, 12:47 AM


General Newbie Member


Group: Forum Members
Posts: 13
Member No.: 1,288
Joined: 16-February 12



Really nice..Thanks for sharing the link..
Top
Acer
  Posted: Oct 20 2012, 07:34 PM


T3H Best Forum Founder/Elite Specialist Master/T0rp3d0Z Ex


Group: Administrator (Main) / Founder
Posts: 16,054
Member No.: 1
Joined: 19-August 04



Awesome tutorial regarding C++, amongst many other things. Keep up the good work, Dark_Jackal!


--------------------
"The new improvement of this place is slightly better than any of my other forums, including the ProBoards one! Please always support my forums whenever you're on and have fun around here! It's the greatest place to post and learn some abilties as well!" -Acer

""Fear is not a factor for you!" -Joe Rogan

"With great power comes great responsibility!" -Spider-Man [Peter Parker's Uncle!]

user posted image
- Click On The Signature Above In My Current Signature For My Old L33test Forums!
user posted image
- Official Member Of The Month Of June 2005 Winner! I'm Proud Of It! <3!
user posted image
- Click On The TrafficSwarm Banner To Get Me More Traffic!
user posted image
- Click On The XFire Banner For My XFire Information!
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
DealsFor.me - The best sales, coupons, and discounts for you

Topic Options



Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.6615 seconds | Archive



Active User Color Legend
Administrator/Creator/Founder/Owner | Co-Administrators | Global/Super Moderators | Female Global/Super Moderators | Regular Board/Forum Moderators | Female Regular Board/Forum Moderators | Graphics Designer Moderation Team | Coding Designers Moderation Team | Staff | Support Teams | Female Members | Forum Members | Elite Legendary Member | Gold Member | Silver Member | Bronze Member | Special Member | Validating | Guests | Banned

Affiliate Box