C++ uses the data stream model for input and output. The data stream for this is handled in its own library. Data streams are responsible for the transport of the data. To be able to use inputs and outputs, the input and output library iostream must be implemented at the beginning of the program code.
The simplest screen output is handled in C++ as follows: cout << "some output";
To store screen input in variables use the following command: cin >> variable-name;
#include <iostream>
using namespace std;
int main(){
int a = 4, b = 6;
cout << "Output ";
cout << "stream" << endl;
cout << "new line" << endl;
cout << "new line\n";
cout << a + b << endl;
cout << "Number: " << 6;
return 0;
}
Output stream
new line
new line
10
Number: 6
#include <iostream>
includes the iostream
library, which provides input and output functionality in C++.using namespace std;
declares that we’re using the std
namespace, which contains various standard C++ functions and objects, including cout
.int main()
function is the entry point of the program. It returns an integer value, typically 0
, indicating successful execution.main()
function, two variables a
and b
are declared and assigned the values 4
and 6
, respectively.cout << "Output ";
uses the <<
operator to insert the string “Output ” into the output stream, which will be displayed on the console.cout << "stream" << endl;
inserts the string “stream” into the output stream and then appends a newline character to start a new line.cout << "new line" << endl;
is similar to the previous line and adds another line of output.cout << "new line\n";
inserts the string “new line” into the output stream, and the escape sequence \n
represents a newline character.cout << a + b << endl;
performs the addition of a
and b
and inserts the result into the output stream. The endl
manipulator is used to append a newline character.cout << "Number: " << 6;
concatenates the string “Number: ” with the integer 6
and inserts it into the output stream.return 0;
statement ends the main()
function and returns 0
to the operating system, indicating successful program execution.#include <iostream>
using namespace std;
int main () {
int integerVariable = 0;
cout << "Please enter a number: " << endl;
cin >> integerVariable; // read in variable
// variable output
cout << "Entered number: " << integerVariable << endl;
}
Please enter a number:
12
Entered number: 12
#include <iostream>
includes the iostream
library, which provides input and output functionality in C++.using namespace std;
declares that we’re using the std
namespace, which contains various standard C++ functions and objects, including cin
and cout
.int main()
function is the entry point of the program. It returns an integer value, typically 0
, indicating successful execution.main()
function, an integer variable integerVariable
is declared and initialized to 0
.cout << "Please enter a number: " << endl;
uses the <<
operator to insert the string “Please enter a number: ” into the output stream, which prompts the user to enter a number.cin >> integerVariable;
uses the >>
operator to read a value from the user and store it in the integerVariable
variable. This allows the user to input a value.cout << "Entered number: " << integerVariable << endl;
uses the <<
operator to insert the string “Entered number: ” into the output stream, followed by the value of integerVariable
, and then appends a newline character. This line displays the entered number back to the user.#include <iostream>
using namespace std;
int main () {
int x;
char c;
cout << "Please enter an integer followed by a character: " << endl;
cin >> x >> c; // read in variable
// variable output
cout << "Integer: " << x << "\nCharacter: " << c << endl;
}
Please enter an integer followed by a character:
5
z
Integer: 5
Character: z
#include <iostream>
includes the iostream
library, which provides input and output functionality in C++.using namespace std;
declares that we’re using the std
namespace, which contains various standard C++ functions and objects, including cin
and cout
.int main()
function is the entry point of the program. It returns an integer value, typically 0
, indicating successful execution.main()
function, two variables are declared: x
, which is an integer, and c
, which is a character.cout << "Please enter an integer followed by a character: " << endl;
uses the <<
operator to insert the prompt message into the output stream, asking the user to enter an integer followed by a character.cin >> x >> c;
uses the >>
operator to read input from the user. The first input will be stored in the x
variable, and the second input will be stored in the c
variable. The user is expected to provide an integer followed by a character, separated by whitespace.cout << "Integer: " << x << "\nCharacter: " << c << endl;
uses the <<
operator to insert the output messages into the output stream. It displays the entered integer value and character value, each preceded by an appropriate label. The \n
escape sequence is used to start a new line, and endl
is used to append a newline character.