Input and Output

COS1511 - Introduction To Programming I · C++ Basics

Input and Output

Input and output (I/O) are essential operations in programming. In C++, you can read data from the user (input) and display data to the user (output). This topic will cover how to perform these operations using C++.

Standard Input and Output Streams

C++ uses streams to handle input and output. The standard input stream is cin, and the standard output stream is cout. You can use these streams to read from and write to the console.

Remember: Use cin for input and cout for output.

Using cout for Output

To display output, you can use the cout object along with the insertion operator <<. Here is an example:

#include <iostream> // Include the iostream library
using namespace std;

int main() {
    cout << "Hello, World!" << endl; // Output a string
    return 0;
}

In this example, the program displays the message Hello, World! on the screen. The endl manipulator is used to insert a new line.

Using cin for Input

To read input from the user, you can use the cin object along with the extraction operator >>. Here is an example:

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: "; // Prompt for input
    cin >> age; // Read an integer from the user
    cout << "You are " << age << " years old." << endl; // Display the age
    return 0;
}

In this example, the program prompts the user to enter their age. It then reads the input and displays it back to the user.

Watch out: Ensure that the variable type matches the input type. If you read an integer into a string variable, it will cause an error.

Multiple Inputs and Outputs

You can read multiple values in a single line or display multiple values in one statement. Here is an example:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b; // Read two integers
    cout << "Sum: " << (a + b) << endl; // Display the sum
    return 0;
}

In this example, the user is prompted to enter two numbers. The program reads both numbers and calculates their sum.

Watch out: When reading multiple inputs, ensure you separate them with spaces.

Formatting Output

You can format the output to improve readability. The iomanip library provides manipulators for formatting. For example, you can set the number of decimal places:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double pi = 3.14159;
    cout << fixed << setprecision(2); // Set precision to 2 decimal places
    cout << "Value of pi: " << pi << endl; // Display pi
    return 0;
}

In this example, the program sets the output precision to two decimal places before displaying the value of pi.

Common Input and Output Errors

While working with input and output, you may encounter common errors. Here are some to watch for:

  • Using the wrong data type for input.
  • Forgetting to include the iostream library.
  • Not using the correct operators (<< for output and >> for input).

Example Program

Here is a complete example that combines input and output:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    string name;
    int age;
    double salary;

    cout << "Enter your name: ";
    cin >> name;
    cout << "Enter your age: ";
    cin >> age;
    cout << "Enter your salary: ";
    cin >> salary;

    cout << fixed << setprecision(2);
    cout << "Hello, " << name << "! You are " << age << " years old and your salary is R" << salary << "." << endl;

    return 0;
}

This program prompts the user for their name, age, and salary, then displays a greeting with the provided information.

Conclusion

Input and output in C++ are straightforward using cin and cout. You can read and display data easily with the correct syntax. Remember to format your output for better readability and ensure that the data types match when reading input.

  • Use cin for input and cout for output.
  • Separate multiple inputs with spaces.
  • Format output using iomanip for better readability.

Check your understanding

  1. What is the purpose of the cin object in C++?
  2. How can you set the number of decimal places in output?
  3. What happens if you try to read an integer into a string variable?
  4. Write a simple program that asks for a user's favourite colour and displays it.