• Log in or sign up for an account here: https://www.sythe.org/login

Introduction to C++

Visual Basic Matt

Apprentice
Joined
Jan 29, 2008
Sythe Gold
56
Messages
647
Location
Evurar
First you'll need a compiler. I personally use Dev-C++ because it seems to work fine.

Download Dev-C++.
Dev1.jpg


Select your language, hmm...
Dev2.jpg


Finish Installation.
Dev3.jpg


Select your theme, I personally think this to be pretty neat.
Dev4.jpg


Wait as it configures/parses all the files.
Dev5.jpg


Be patient as it loads up for the first time.
Dev6.jpg


You always will get these neat little tips, I suggest you read them as they are a great help on getting to understand the program.
Dev7.jpg


Now it is officially installed and ready to use.

First to start out you need to open a new source file.
You can do this by going to file/New/Source File, or by simply pressing Ctl+N.

You will see a blank screen where your code will go.
Starting out you must remember one thing, in all of your programs you must put the following inclusion at the top of all your code, which is where you will be including other functions aswell.
Code:
#include <iostream>
Your screen should look like this.
Dev8.jpg


When you include something like that, for instance, lets say you wanted the program to use mathematical methods without having to create your own. You can import cmath, which would look like this.
Code:
#include <cmath>
You would just have to add that to the top of your code and this would make it very easier to do things without creating your own functions. This is just one of the many functions you can include to your project. Although you Must always include the iostream in all of your projects.

Next you will start off with the main function of your program. This will be triggered the second the user opens the application.
Code:
int main()
{



}
That is the main function that should always be the first line of code in your program after all of the includes and your variable declaration. Your code will lie in between the brackets "{" and "}".

Inside the brackets you should start out with the following code.
Code:
using namespace std;
This will allow the program to communicate with the user.

First off, you will need to declare your variable.
Code:
int x;
This will tell C++ that x is your integer. You can now make the variable "x", any value you wish.

There is two main communications with C++ and the user. They would be "Cin" and "Cout". You may ask. What does that mean? Well, The "C" stands for console because we are making a console application. In is to ask the user for a variable of some sort. Out is to output the info to the user. So lets say we wanted to ask the user what their favorite number was. We would use the "Cout" command to ask the user the question.
Code:
cout << "What is your favorite number:";
So now the user knows that he/she needs to input here favorite number, but the application must be able to read what he/she inputs. That is why we have the "Cin" command.
Code:
cin >> x;
You will notice that there is a ";" after all of the lines of code here, well this is what tells C++ that it is done at that line of code, and doesn't need to be continued. But be careful, during if commands and many other commands you may do, the line isn't finished doing its job yet, therefore it needs to continue on until its done.

Well, now we told the program to read what the user put in, but what will we do with that number? Well, we can do a million things with a number, but just for now, lets have the program tell the user the number. We will do this with the "Cout" command to display it to the user.
Code:
cout << "Your favorite number is " << x << "." << endl;
You should notice how their is "<" and ">" well, simply there opposite so when you do "Cin" and "Cout they will be opposites. Another thing is that anything in the """ will be recognized as text given to the user and without them, the program will know to go figure out what the variable "x" is, or whatever the variable is. One last thing you will see is "endl" That simply ends the line so the next piece of code will not be upon it.

If you were to compile and run it, it would disapear really fast after the user inputs his/her favorite color. This is because we need to tell C++ to pause the program at its current line. You would just add this bit of code.
Code:
system("pause");

Then finally you will need to have the program stop itself, or just finialize everything, just add the following to the bottom of your function.
Code:
return 0;

Your code should finally look something like this.
Dev9.jpg


Now that your done test it out by going to Execute/Compile and run or simply press F9.

It should end up looking like this.
Dev10.jpg



This was my first programming tut, I hope you enjoyed it. Please comment/rate and I'll try and make more. I know I'm visual basic matt but I don't just program in vb. :p Thanks!
 
Leeched tut, Same one is on cs.washington. Good effort in adding some word's "Select your language" "Hmm..."
 
Although you Must always include the iostream in all of your projects.
Please remove this, completely untrue...

Inside the brackets you should start out with the following code.
Code:
using namespace std;
It should appear outside of your main function below the inclues...

This will tell C++ that x is your integer. You can now make the variable "x", any value you wish.
You can now make "x" any numerical value that doesn't contain a fraction and that is within the lowest possible value and max value an integer can contain.

system("pause");
Use getchar() to pause the program and view output as its an actual standard and cross-platform. System("pause") only works on Windows and isn't defined on a majority of compilers.
 
Member of the Month Winner
Sythe's 10th Anniversary
Please remove this, completely untrue...


It should appear outside of your main function below the inclues...


You can now make "x" any numerical value that doesn't contain a fraction and that is within the lowest possible value and max value an integer can contain.


Use getchar() to pause the program and view output as its an actual standard and cross-platform. System("pause") only works on Windows and isn't defined on a majority of compilers.
I see what you did there...

More importantly Matt, I'm not trying to be rude but learn what you're talking about before you talk about it.
 
Runescape Gamblingrunescape gambling
Back
Top