Basic Input/Output in cpp

Input/Output (I/O) refers to the communication between a program and external sources, such as a user or a file. In programming, I/O plays a crucial role in interacting with users, displaying output, and processing input data. It allows programs to receive input and produce output, making them more interactive and useful.

In C++, the standard input stream (cin) and output stream (cout) are essential tools for I/O operations. The cin stream is used to read input from the user, while the cout stream is used to display output on the screen. These streams simplify handling user input and generating program output, making C++ programs more versatile and user-friendly.

Basic Input Operations in C++

A. Reading Input using cin

The cin stream and the extraction operator (>>) are used to read input from the user. Here are the basic steps for reading different data types:

Reading different data types: int, float, double, char, etc.

int age;
cout << "Enter your age: ";
cin >> age;
float weight;
cout<< "Enter your weight: ";
cin >> weight;
char grade;
cout << "Enter your grade: ";
cin >> grade;

Using the extraction operator (>>) to extract input values

int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;

Handling input errors and invalid input

int age;
cout<< "Enter your age: ";
cin >> age;
if (cin.fail()) {
    cout << "Invalid input! Please enter a valid age.";
    cin.clear(); // Clear the error flag
    cin.ignore (numeric_limits<streamsize>::max(), '\n');
}

B. Input Validation and Error Handling

Using conditional statements to validate input

int num;
cout << "Enter a positive number: ";
cin >> num;
while (num <= 0) {
    cout << "Invalid input! Please enter a positive number: ";
    cin >> num;
}

Handling invalid input using loops and error flags

int num;
cout << "Enter an integer: ";
while (!(cin >> num)) {
    cout << "Invalid input! Please enter an integer: ";    
    cin.clear();
    cin.ignoze(numeric_limits<streamsize>::max(), '\n');
}

Clearing the input buffer to prevent unwanted behavior

int num;
char letter;
cout<< "Enter an integer: ";
cin >> num;
cout << "Enter a letter: ";
cin >> letter;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

These examples demonstrate how to read input using cin, handle invalid input, and clear the input buffer when necessary. It is important to validate input and handle errors gracefully to ensure the smooth execution of the program.

Basic Output Operations in C++

A. Writing Output using cout

To display output in C++, the cout stream is used. Here are the basic steps for writing output:

Outputting data types: int, float, double, char, strings, etc.

int age 25;
cout << "Age: " << age << endl;
float weight = 65.5;
cout << "Weight: " << weight << endl;
char grade = 'A';
cout << "Grade: " << grade << endl;
string name = "John Doe";
cout << "Name: " << name << endl;

Formatting output using escape sequences and manipulators

cout << "Hello\tWorld\n"; // Using escape sequences for tabs and newlines
int num= 123;
cout << "Number: " << num << endl;
cout << "Number in hexadecimal: " << hex << num << endl;

Displaying text and variables in the output

string name = "Alice";
int age = 30;
cout << "Name: "<< name << ", Age:" << age << endl;

B. Formatting Output with Manipulators

Manipulating output precision, width, and fill characters

double value = 3.14159;
cout.precision(3);
cout << "Value: " << value << endl;
cout.width(10);
cout << "Value: " << value << endl;
cout.fill('*');
cout << "Value: " << value << endl;

Formatting numeric output with the fixed, scientific, and general notation

double value = 1234.5678;
cout << "Fixed Notation: " << fixed << value << endl;
cout<< "Scientific Notation: " << scientific << value << endl;
cout<< "General Notation: " << defaultfloat << value << endl;

Aligning output using setw and setfill manipulators

string name = "Alice";
int age = 25;
cout << left << setw(10) << setfill('*') << name;
cout << right << setw(5) << setfill('#') << age <<< endl;

These examples demonstrate how to use cout to write output in C++ and format the output using escape sequences and manipulators. Using these techniques, you can effectively display text, variables, and formatted data to the user.


Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs