• Welcome to ForumKorner!
    Join today and become a part of the community.

Beginners C++ Tutorial 2: Variables and the If statement.

Gonzo

Onyx user!
Reputation
0
c++ Begginers Tutorial 2 - Varaibles and IF statements.

So first of all we are going to start with the code from part 1:

Code:
//Includes
#include <iostream>

//Namespace
using namespace std;

//Main
int main()
{

    cout << "Hello World!" << endl; //Print hello world
    system("PAUSE"); //Wait for a keypress
    return 0;

}

If you do not fully understand this code then please go and complete or at least read through my 1st c++ tutorial.

If you do understand, then lets begin!

Unlike some programming languages, in c++ a variable MUST be declared before it can be used, however you can also define its type and value within declaration.
There are a few data types which you can use (they can be found here) but today we will focus on:
  • String: A string is a data type which can hold letters and numbers, and is useful for storing general input.
  • Int: An int (integer) is a data type which can store whole numbers, any decimal numbers will be rounded.
  • Double: A double is a data type which can hold decimal numbers.

Now, i am going to show you some example code on how to declare them:

Code:
int number; //Declares the variable "number" as an 'int' with no value
string name = "Gonzo"; //Declares the variable "name" as a 'string' with the value "Gonzo"
double secondNumber = 0.0; //Declares the variable "secondNumber" as a 'double' with the value "0.0"

Great!, now you know how to declare and set values to some variables, lets use them to make a basic addition or subtraction calculator!

First of all, declare a string:

Code:
operator1

and two ints:

Code:
firstNumber
secondNumber

After you have done that the code should look something like this:

Code:
//Includes
#include <iostream>

//Namespace
using namespace std;

//Main
int main()
{
    //Variables
    string operator1;
    int firstNumber;
    int secondNumber;

    cout << "Hello World!" << endl; //Print hello world
    system("PAUSE"); //Wait for a keypress
    return 0;

}

Also you can go ahead and take out the hello world code (from "cout" to "return 0;")
Great, now you have some variables that we can start setting values for.
Before we continue, i am going to explain the "if","else if" and "else" functions.
Heres an example:

Code:
if (the car is red)
	paint the car blue
else if (the car is blue)
	paint the car red
else
	Do not paint the car

Hopefully my terrible example helped you understand a bit. If not, don't worry it is easier than it looks.
So lets actually make our calculator do something!
Put in this code after your variables:

Code:
if (operator1 == "+") //If the variable "operator1" is equal to "+"
{
	cout << firstNumber + secondNumber;	//Print "Answer" on the screen
	cin.get(); //Pause
}
else if (operator1 == "-") //If the variable "operator1" is equal to "-"
{
	
	cout << firstNumber - secondNumber; //Print "Answer" on the screen
	cin.get(); //pause
}

Hopefully the comments should explain the code just fine, if not then please just carry on with the tutorial and everything should become clear.
So so far the code should be:


Code:
//Includes
#include <iostream>

//Namespace
using namespace std;

//Main
int main()
{
    //Variables
    string operator;
    int firstNumber;
    int secondNumber;

	if (operator1 == "+") //If the variable "operator1" is equal to "+"
	{

		cout << firstNumber + secondNumber;	//Print "Answer" on the screen
		cin.get(); //Pause
	}
	else if (operator1 == "-") //If the variable "operator1" is equal to "-"
	{
		cout << firstNumber - secondNumber; //Print "Answer" on the screen
		cin.get(); //pause
	}

return 0;

}

Now we will change some variables to see what out code outputs.
Change:
operator = +
firstNumber = 2
secondNumber = 4
And you should get the number 6 printed in the console.

Well thats it for this tutorial! Hope you likes it, and you can find this tutorial and my others on www.carbzo.org
 

Buddy_mybb_import10515

Active Member
Reputation
0
Maybe I should have looked through the section for your other tutorials. D;
Thanks for taking the time to make this and keep 'em coming.
 
Top