C++ uses std::cout to print output text or values to the console.
std::cout does not insert a new line at the end of the output.
If we want to insert a new line to the output, we have to use std::endl.
Syntax:
std::cout << "Some random text" << std::endl;
The syntax above is used to print the text inside the " " and insert a new line after
the word text. The output will be some random text and a new line below it. The
word std means we can use names and objects from the standard library. In this case,
the cout function.
std::cout can also be used to print the value of a variable.
Syntax: std::cout << variable_name;
Example:
#include <iostream> int main() { int myNum = 19; std::cout << "Hello World!" << std::endl; std::cout << myNum; return 0; }
Output:
Hello World!
19
We can also omit the std:: in front of the cout.
But, we have to replace it with using namespace std; before the main function.
The code below has the same output as the previous code, but it uses using namespace std;
instead.
Example:
#include <iostream> using namespace std; int main() { int myNum = 19; cout << "Hello World!" << endl; cout << myNum; return 0; }
In C++, you can use the cin function to get user input.
cin can also get multiple inputs by separating the inputs
by the operator >>.
Remember that cin uses the operator >>,
while cout uses the operator <<.
Syntax:
std::cin >> input1 >> input2 >> inputn;
Example:
#include <iostream> int main() { int myNum; char myChar; std::cout << "Input a number and a character: "; std::cin >> myNum >> myChar; std::cout << myNum << myChar; return 0; }
Output:
Input a number and a character: 23 C 23C
cin can also take string input, but it will only accept the string until it sees a
white space. To avoid this error, we use getline(std::cin, variable_name) to get the
whole line of input
instead of just cin. We can still
use cin only if the string does
not contain any white space between the contents. Be sure to
include the <string> library too because
we are using the std::string type of variable.
Example:
#include <iostream> #include <string> int main() { std::string myName; std::cout << "Input your full name: "; getline(std::cin, myName); std::cout << myName; return 0; }
Output:
Input your full name: John Doe John Doe
If we don't use the getline function, the
myName variable will only contains John, not John Doe.
Comments can be used to explain what does a block of code do. Comments can also be used to prevent the execution of some lines of code. There are two types of comments in C++:
Any text between // and the end of the line will not be
executed by the compiler.
Syntax: //some comments here
Any text between /* and */ will not be
executed by the compiler.
Example:
/* Hello world! Some comment here */
Example:
#include <iostream> #include <string> int main() { std::string myName; //this line will not be executed std::cout << "Input your full name: "; getline(std::cin, myName); /* std::cout << "hello"; The console will not show "hello" because the std::cout << "hello" is in the comment and will not be executed. */ return 0; }
Escape sequences are mostly used to control or format the output. It started with a backslash (\) and followed by another character.
Example:
#include <stdio.h> int main() { std::cout << "Hello,\nWorld!"; /*The output will be Hello, World! Instead of Hello,World! */ return 0; }
Below are the most commonly used escape sequences.
| Escape Sequences | Description |
|---|---|
| \n (newline) | Insert a new line on the output (have the same use as std::endl). |
| \t (tab) | Insert a horizontal tab on the output. |
| \\ (backslash) | Backslash can't be printed directly as it
is used for escape sequences in C++, so we have to place another backslash following the backslash used for an escape sequence. So, to print backslash, we type \\ not \ |
| \' (single quote) | Print a single quote in the output |
| \" (double quote) | Print a double quote in the output |
| \? (question mark) | Print a question mark in the output |
| \b (backspace) | Move the cursor one position back in the current line of text. |
| \a (alert or bell) | A beep is generated by the computer on execution. |
| \r (carriage return) | Move the cursor to the beginning of the current line. |
| \v (vertical tab) | Insert a vertical tab on the output. |
An exception is an exceptional circumstance that appears when a program is executed. For example, division by zero, invalid inputs, and many more. C++ exception handling consists of three major keywords:
throwSyntax: throw n;trytry block identifies which block of code will be tested
and performed exception-catching. The keyword try has to be
followed by one or more catch keywords to perform the exception-catching.
catchcatch keyword indicates the catching of an exception.
Syntax:
try { // protected code } catch( ExceptionName e1 ) { // code to handle the exception e1 } catch( ExceptionName e2 ) { //code to handle the exception e2 } catch( ExceptionName eN ) { // code to handle the exception eN }
Example:
#include <stdio.h> /*below is the division function that is defined by the user. You can learn more about defining your own function in the function page */ double division(int a, int b) { if( b == 0 ){ throw "Division by zero!"; } return (a/b); } int main () { int x = 50; int y = 0; double z = 0; try { z = division(x, y); std::cout << z << std::endl; } //test the above code block catch (const char* msg) { std::cerr << msg << std::endl; } /*if the result type is const char* (the result is the message "Division by zero!"), then cerr (print error message) of the msg. In this case, msg is "Division by zero!" */ return 0; }
The output of this block of code is Division by zero!.