I will post multiple examples in one topic to help save space, and to better categorize what I put on here. I will also throw tags like [CPP] in my topics so that it will be easier to find with search.
so here we go.
this bit explains some small stuff. It also explains a cheat that I like to use from time to time
#include //standard c++ header file for input and output commands
using namespace std; //removes the use of using std::
int main() // main function of program
{
// a // starts a single line comment
// /* */ starts and ends a multi line comment
/*
this is a multi line comment
see?
*/
cout << "Hello World\n"; //prints Hello world to screen
getchar(); //pauses program untill a key is entered so that messagage can be read
return 0; //returns successful code execution
}
this bit explains a bit about variables and a tiny part of using strings:
#include
#include
using namespace std;
int main()
{
/*
there are many different kinds of variables. The ones that we will discuss here
are the character and integer variables and how to define them.
char is how you would define a variable that would have a character value.
int is how you would define a variable that has a number value.
There are many different forms of ints but we will go over the default for now
and since char wouldn't allow me to use strings we are using the string header
and to implament a string you would do the same as a int or char
*/
string Hello; // implements a string
int number;
char hi = 'h'; // defines a char
Hello = "Hello"; // defines as the string Hello
number = 25000000; // defines number's value as 25million
/*
if you wish to use forward slash use a backslash before it so that it prints.
to start a new line, use back slack then 'n'
*/
cout << Hello << " \/This implements a string" << "\n";
cout << "I am this old: ";
cout << number << " \/this implements an integer\n";
cout << hi << " \/this implements a char\n";
getchar();
return 0;
}
here I give a small demonstration of the different if statements and a little cheat I use when coding in windows:
#include
using namespace std;
int main()
{
/*
this example will go over if statements else if statements and else statements.
we will also practice using variables as well.
this example we will be creating a program that will be a simple Q&A applet
the program asks a question and the user answers
*/
char YorN; //Yes or No variable
cout << "Are you the program owner?: ";
cin >> YorN; //cin collects the data typed and sends it >> to the variable
if(YorN == 'Y') //if YorN is equal to 'Y' then
{
cout << "Hello sir\n";
system("pause"); // only used on windows, on other systems use getchar()
}
else if(YorN == 'N') // if else YorN is equal to 'N' then
{
cout << "Good bye who ever you are!\n";
system("pause");
}
else // else just say wrong choice
{
cout << "invalid answer, please choose either 'Y' or 'N'\n";
system("pause");
}
return 0;
}
In this example I show how to use a while loop
#include
using namespace std;
int main()
{
/*
in this example we will go over while loops.
it can be written many different ways and some
prove useful then others.
I will show you 1 good way
*/
int number;
cout << "please enter a number less then 10: ";
cin >> number;
while (number < 10) // while number is less then 10 add 1 to number and print results untill it hits 10
{
number++;
cout << number << endl; //endl does the same thing as \n
if (number == 10)
break; // breaks out of while loop if number is equal to 10
}
system("pause");
return 0;
}
my forloop example shows how to use a for loop in a basic way
#include
using namespace std;
int main()
{
int i;
cout << "Please enter a number less then 10: ";
cin >> i;
for(i;i<=10;i++) //this for loop is similar to the while loop but will have a better use for most things.
// if you need to just run something in a loop over and over, for ( , , ) does that.
//for loops have more use in c++ then any other loop
{
cout << i << endl;
if (i == 10)
break;
}
system("pause");
return 0;
}
and here is a snipplet I coded to show how to create basic functions
#include
using namespace std;
int mathfunc() //creates a function like int main this func adds 2 numbers and return it
{
int number;
number = 5+5;
return number;
}
int multifunc(int num3,int num4)
{
int num;
num = num3 * num4;
return num;
}
int main()
{
cout << mathfunc() << endl; // the function we made and prints its return value
cout << multifunc(5,5) << endl; //passes 5 and 5 to the num3 and num 4 parameters of multifunct() and prints to screen
system("pause");
return 0;
}