ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Three Simple C++ Programs With Detailed Explanations

Updated on July 15, 2016
Source

Code 1

#include <iostream>
using namespace std;
int main() ]
{
cout << "This is a C++ Program" << endl;
}

From above, what we should all notice first is:

#include<iostream>

The above line called a header file or preprocessing directive.

Rules Governing Header Files In C++

  • All header files must start with the # character.
  • You cannot use a header file twice in a single program.

Don't relent by knowing how to use the iostream header alone, there are others you will use in the future.

If you use linux, you may find the those header files located in usr/include/c++/5.

Now we move on to:

using namespace std;


In this case, I suggest we should assume that this line is our printer that displays out our code with the functions cout and endl

COUT

This displays the body of our main functions

e.g cout << "I am a boy" << endl;

displays I am a boy without quotes.

ENDL
In one word, it's function is just to break a line or jump to the next line.

Now back to our initial explanation. Without using namespace std; , we will have to type std::cout and std::endl everytime we need them. std in this case means standard.

using namespace std; is very important to make our program simple and neat.


int main()

{

This function specifies the the beginning of our program and we are declaring a function named main.

}

It specifies the end of our program. This function tells the complier to stop.

Open and Close curly braces are really so important.

Code 2: Variable Assignment

#include<iostream>
using namespace std;
int main() 
{
int x;
x = 10;
cout << x << endl;
}

The difference here is clear. This code stores an integer value and
then prints the value of the variable.

Declaring x is the number one step.

int x; else you will get an error from the compiler.

x=10;

According to the program, x has a static value of 10 and to print this out, we use the cout function without columns:

cout << x << endl;

It then prints out 10.

Remember that all syntax in C++ must end with ; excluding header file and the main function.

Code 3: Getting Input From User

//This get a whole number from user
#include<iostream>
using namespace std;
int main() 
{
int x;
cout << "Enter a Number: << endl;
cin >> x ;
cout << "You entered " << x << endl;
}

//This get a string of text from user
#include<iostream> 
using namespace std;
int main() 
{
string name;
cout << " What is your name?: << endl;
cin >> name;
cout << " Welcome " << name << endl;
}

cin is a function used to get input from users via Keyboard.

cin >> x; takes in the value of x the user provided, store it temporarily and then displays it out if it needed in the code.

The value of x won't just pop up, you have to use the cout function.

I'd love to hear you opinion about this, comment using a dummy cout function if you understands what I have explained so far. Also, try the simple quiz below. Meanwhile, if you missed my first C++ post, check it here: http://hubpages.com/technology/simplecpp

What is the output of this code if the user inputs 20?

#include<iostream> //header file
using namespace std;
int main() 
{
int x,y; //declares variable x
y=10;
cin >> x; // user input 20
cout <<" x + y " << x + y << endl; // displays the result
}

© 2016 Iyanu Victor

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Anonymous 14 months ago

      I give you 5 star for this wonderful post