First C++ Program
Learn to create and execute the first C++ program.
We'll cover the following...
Hello World
It’s time to write our first program in C++, which will display the message “Hello World!” on the screen.
This is the code widget that we’ll use to write our C++ code. We may execute our C++ program by clicking the “Run” button at the bottom of the widget:
Note: We can add non-executable text in a C++ program by adding a double backslash sign (
//) at the start of the text. This type of text is called a comment. We use comments to include descriptions in the program.
Explanation
-
Line 1: It is a comment because it starts with
//(a double slash). It is only for the readers of this program. It describes the purpose and/or related matters of the program. -
Line 2: The
iostreamenclosed in<and>is the header file that supports the input and output facility. We write#includeat the start of this line and do not use;at the end. -
Line 3: This line defines certain facilities we use in our programs, for example,
cout. This remains at the start of almost every C++ program. -
Line 4: This is blank. C++ does not object to any blank lines. It is used to separate the header portion from the main program visually.
-
Line 5: This is the start of the executable part of the program. Every executable C++ program has a
main. It is the entry/starting point of every C++ program. -
Line 6: This is the start of the main block of code. Every starting brace
{has a corresponding closing brace}, as seen in line 9. -
Line 7: This is the only line that caused the program to give output. It corresponds to the purpose of the whole program. Every other line is in support of this line. Whenever we want to display, we use
coutwith<<representing an arrow to show the direction of text going tocout. Therefore,cout <<displays the text. -
Line 8: This terminates the program and remains the same in almost every C++ program.
-
Line 9: This is the end of the main block of code. It corresponds to the opening brace at Line 6.
New line in the output
There are different ways of giving a line break in the output, as demonstrated in the following code. For a better understanding, you can take the AI mentor’s help.
Explanation
- Lines 6–7: The text of these lines is on the same line in the output. The
\nsymbol, termed as a new line character, causes the line break. - Line 8: This shows the use of
\nas a separate text. - Line 9: This illustrates the use of
endlfor giving a line break in the output, as demonstrated from the output of line 10.
It is important to notice that endl cannot be used as part of the text; it is always used separately.
Personalize messages and add variables
Let’s write another C++ program that inputs the name of the user before displaying a greeting message. We use the variable, yourName, to store the input given by the user in C++.
A variable name can only contain letters (
A–Zanda–z) and the underscore symbol (_). It may also contain digits (0–9), but the variable name can’t start with those. For example,Key_2is a valid variable name but2_keyis not. Variable names are case-sensitive, meaning thatname,Name, andNAMEare three different variables.
In C++, we use cin for input (to read a value from the user), and cout for print (to display a value on the screen as output).
The program.cpp in the left pane of the following widget is the name of the program file. The .cpp indicates that it’s a C++ program. When we press the “Run” button to execute the program, it shows a black screen called the terminal, where we can input values and see the resulting output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string yourName;
cout << "Enter your name: ";
cin >> yourName;
cout << "Hello " << yourName << endl; // Printing User's name with Hello
cout << "Welcome to Educative" << endl;
return 0;
}The word exit at the end of the output indicates the completion of the program in the terminal.
The code above displays the message "Hello " before the value of the yourName variable on the first line. The space after Hello is part of the label to keep a space before the value of yourName in the output. Then, it displays “Welcome to Educative” on the second line.
Note: We use
<and>withstringin line 2, but not on line 7.
Explanation
- Line 2: This includes a header file
string. The header filestringdefines the functionality for string variables to store text values in C++. This line remains as it is, in programs where the string variable is used. - Line 7: This defines a string variable,
yourName, to store the value input by the user. - Line 9: This allows the user to input a value. We use
cinwith>>representing an arrow for showing the direction of text, coming fromcin. Therefore,cin >>inputs the text. - The other lines can be understood from the explanation of the previous program.
The cin >> reads the text till the first space. For example, if the input text is Learn to code, it will read only Learn. To input the text with spaces, we use getline in C++ as demonstrated in the following code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string yourName;
cout << "Enter your full name: ";
getline(cin, yourName); // Inputting full name
cout << "Hello " << yourName << endl; // Printing User's name with Hello
cout << "Welcome to Educative" << endl;
return 0;
}We have used getline(cin, yourName) in the code above, which allows spaces as input into the variable yourName.
C++ programming practice
Let’s practice writing the first C++ program that takes multiple inputs from the user.
Tell a story
Let’s write a program that tells a story with information given by the user. The program should ask the user to enter their name, age, city, college, profession, and pet’s name.
Once we are done writing the program, we can execute by clicking the “Run” button. The program should display the prompts to enter an input on the terminal, and we can enter the required input. Remember to store the input and print it using cout, where required. The program should display the following story, inserting the user’s input into the appropriate locations in the following text:
There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted an animal named PETNAME. They both lived happily ever after!
#include <iostream>
int main()
{
// write your code here
return 0;
}