In C++, cout sends formatted output to standard output devices, such as the screen. We use the cout object along with the
#include using namespace std; int main() < // prints the string enclosed in double quotes cout
Output
This is C++ Programming
How does this program work?
Note: If we don't include the using namespace std; statement, we need to use std::cout instead of cout .
This is the preferred method as using the std namespace can create potential problems.
However, we have used the std namespace in our tutorials in order to make the codes more readable.
#include int main() < // prints the string enclosed in double quotes std::cout
To print the numbers and character variables, we use the same cout object but without using quotation marks.
#include using namespace std; int main() < int num1 = 70; double num2 = 256.783; char ch = 'A'; cout
Output
70 256.783 character: A
Notes:
cout
In C++, cin takes formatted input from standard input devices such as the keyboard. We use the cin object along with the >> operator for taking input.
#include using namespace std; int main() < int num; cout > num; // Taking input cout
Output
Enter an integer: 70 The number is: 70
In the program, we used
cin >> num;
to take input from the user. The input is stored in the variable num . We use the >> operator with cin to take input.
Note: If we don't include the using namespace std; statement, we need to use std::cin instead of cin .
#include using namespace std; int main() < char a; int num; cout > a >> num; cout
Output
Enter a character and an integer: F 23 Character: F Number: 23