We'll start with integers (int).
First, at the beginning of the program, you must declare the variable: we'll call it "firstvariable." This is how you declare it:
int firstvariable;
Then, after it is declared, you can change its value:
firstvariable=22;
Alternatively, you can declare the variable with a value already assigned (we'll call this "secondvariable"):
int secondvariable=22;
Now that the variable is declared, you can display it using cout:
cout<<firstvariable<<endl;
*Note that no quotation marks were used since it is a variable; this displays "22."
cout<<"firstvariable";
You can also assign a value to your variable using cin:
cin>>firstvariable;
cout<<"The value of your variable is "<<firstvariable;
cout<<firstvariable;
To make a new line, use "\n," or endl:
cout<<"\nThat value again is "<<firstvariable<<"\n";
cout<<"That value yet again is "<<firstvariable<<endl;
To make a quotation mark, use \":
cout<<"Here is a word in quotes: \"a word\""<<endl;
Now, lets get two variables and add them:
cout<<"Enter any number\n>";
cin>>firstvariable;
cout<<"Okay, now enter another\n>";
cin>>secondvariable;
cout<<"The sum of the two numbers is "<<firstvariable+secondvariable<<endl;
Alternatively, you could add the variables first and then print them:
int sum=secondvariable+firstvariable;
cout<<"The number remains the same: \""<<sum<<"\"";
cout<<"The product is "<<firstvariable*secondvariable<<endl;
cout<<"The quotient is "<<firstvariable/secondvariable<<endl;
cout<<"The difference is "<<firstvariable-secondvariable<<endl;
So, "int" is an integer (e.g. -1000, 993, 12, 0):
int firstinteger=99;
And, "bool" is a true or false statement:
bool firstbool=true;
bool secondbool=false;
Then, "string" is a string of characters:
string firststring="Hey there, this is your string";
cout<<firststring<<endl;
Also, "char" can be used for text but it generally isn't. It may be avoided.
Finally, "void" is actually the absense of a variable; you'll see its use in Lesson 5 on functions, however.
The Code
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main ()
{
/*A variable is something that can change. Many different types of variable exist; these are the most common: int, bool, string, and char.
We'll start with integers (int).
First, at the beginning of the program, you must declare the variable: we'll call it firstvariable. This is how you declare it:*/
int firstvariable;
//Then, after it is declared, you can change its value:
firstvariable=22;
//Alternatively, you can declare the variable with a value already assigned (we'll call this secondvariable):
int secondvariable=22;
//Now that the variable is declared, you can display it using cout:
cout<<firstvariable<<endl; //note that no quotation marks were used since it is a variable; this displays "22."
cout<<"firstvariable";//quotes were used to demonstrate that it will display "firstvariable."
//You can also assign a value to your variable using cin:
cout<<"\nEnter the value for your variable\n>";//"\n" places a new line after the displayed text.
cin>>firstvariable;
cout<<"The value of your variable is ";
cout<<firstvariable;
cout<<"\nThat value again is "<<firstvariable<<"\n";
cout<<"That value yet again is "<<firstvariable<<endl; //endl makes a new line like "\n"
cout<<"Notice that when a quotation mark is typed, it will end what is displayed..."<<endl;
//To make a quotation, use \"
cout<<"Here is a word in quotes: \"a word\""<<endl;
cout<<"Now, lets get two variables and add them\nEnter any number\n>";
cin>>firstvariable;
cout<<"Okay, now enter another\n>";
cin>>secondvariable;
cout<<"The sum of the two numbers is "<<firstvariable+secondvariable<<endl;
cout<<"Alternatively, you could add the variables first and then print them:"<<endl;
int sum=secondvariable+firstvariable;
cout<<"The number remains the same: \""<<sum<<"\"";
cout<<"The product is "<<firstvariable*secondvariable<<endl;
cout<<"The quotient is "<<firstvariable/secondvariable<<endl;
cout<<"The difference is "<<firstvariable-secondvariable<<endl;
//So, int is an integer (e.g. -1000, 993, 12, 0)
int firstinteger=99;
//bool is a true or false statement
bool firstbool=true;
bool secondbool=false;
//string is a string of characters
string firststring="Hey there, this is your string";
cout<<firststring<<endl;
/*char can be used for text but it generally isn't. It may be avoided.
void is actually the absense of a variable; you'll see why it was included in the variables section in Lesson 5 on functions, however.
void, int, bool, string, and char.*/
cin.ignore();cin.get(); //remember, this makes your program not close without input
return 0;
}