Input / output

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;

Output in C++

Code Example

#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
Output stream
new line
new line
10
Number: 6

Code Explanation

  1. Inside the main() function, two variables a and b are declared and assigned the values 4 and 6, respectively.
  2. The line cout << "Output "; uses the << operator to insert the string “Output ” into the output stream, which will be displayed on the console.
  3. The line cout << "stream" << endl; inserts the string “stream” into the output stream and then appends a newline character to start a new line.
  4. The line cout << "new line" << endl; is similar to the previous line and adds another line of output.
  5. The line cout << "new line\n"; inserts the string “new line” into the output stream, and the escape sequence \n represents a newline character.
  6. The line 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.
  7. The line cout << "Number: " << 6; concatenates the string “Number: ” with the integer 6 and inserts it into the output stream.
  8. Finally, the return 0; statement ends the main() function and returns 0 to the operating system, indicating successful program execution.

Mixed Example

Code Example

#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;
}
Output
Please enter a number: 
12
Entered number: 12

Code Explanation

  1. Inside the main() function, an integer variable integerVariable is declared and initialized to 0.
  2. The line 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.
  3. The line 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.
  4. The line 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.

Multiple Inputs

Code Example

#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;
}
Output
Please enter an integer followed by a character: 
5
z
Integer: 5
Character: z

Code Explanation

  1. The line #include <iostream> includes the iostream library, which provides input and output functionality in C++.
  2. The line using namespace std; declares that we’re using the std namespace, which contains various standard C++ functions and objects, including cin and cout.
  3. The int main() function is the entry point of the program. It returns an integer value, typically 0, indicating successful execution.
  4. Inside the main() function, two variables are declared: x, which is an integer, and c, which is a character.
  5. The line 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.
  6. The line 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.
  7. The line 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.