In general a developed applications, during it’s runtime, it do 3 actions; it gets inputs, do some logical algorithms and puts you outputs. If you read about Structure of C++ Programming Language, it is good to learn how input and output functions are used.
How do I use Outputs in C++?
In C++, cout
object function is used with the <<
operator, direction of arrows shows the output and anything on the right side of this operator goes as an output. In C++ you must use with std namespace like std::cout. In C++ :: operator is used to refer members of a class or namespace, Here std is standard library which has many functions including input and output object functions. See example below,
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main() { std::cout << "Hello World"; return 0; } |
Or you can use std as a namespace at the beginning and you don’t need to type std:: before using all of it’s objects. See example below;
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; } |
Each cout object adds to previous input. You can use many of them, like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int main() { cout << "Hello "; cout << "World."; cout << "This "; cout << "is "; cout << "my "; cout << "testing!"; return 0; } |
In this example output, all cout lines will be combined in one line and the output will be like this;
1 2 3 |
Hello World.This is my testing! |
this example is same with;
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { cout << "Hello World.This is my testing!"; return 0; } |
You can write each of them in one line by using << operator. See this example, it has same output as above too;
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { cout << "Hello " << "World." << "This " << "is " << "my " << "testing!"; return 0; } |
This method is easy to output variables. You can output texts and variables together as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; int main() { int i = 5; float f = 3.14; string s = "Hello World"; cout << "String:" << s << " Integer:" << i << " Float:" << f; return 0; } |
So, how we can start from the next line, like when you press enter key. Here we use ‘\n’ character at the end or inside the string at the end, We can use endl which shows end of line too. All are same mostly \n is used, because endl See examples below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; int main() { int i = 5; float f = 3.14; string s = "Hello World\n"; cout << "String:" << s ; cout << "Integer:" << i << '\n'; cout << "Float:" << f << endl; cout << "All is listed\n" return 0; } |
How do I use Inputs in C++?
In C++, cin
object function is used with the >> operator, direction of arrows shows the output is the variable and anything on the right side of this operator is input. In C++, if you don’t use namespace you must use with it’s std class like std::cout. In C++ :: is used to refer members of a class, Here std is namespace which has input and output object functions. See example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { int num; std::cout << "Type an integer number:"; std::cin >> num; std::cout << "Integer number you typed is:" << num; return 0; } |
Or you can use std as a namespace at the beginning and you don’t need to type std:: before using all of it’s objects as given before. See example below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; int main() { int num; cout << "Type an integer number:"; cin >> num; cout << "Integer number you typed is:" << num << '\n'; return 0; } |
This last example is also good to summarize all output and input mechanism in C++.
Head over and download Dev-C++ for modern C++ development on Windows.