C++.Module 01
C++.Module 01
cout << “ … “;
Note:
▪ cout -> Character Output
▪ Defined in the <iostream> header file.
▪ The insertion operator (<<)is used to send data to the standard output (on the console).
▪ ‘endl’ is used to insert a newline character and flush the output buffer.
▪ ‘\n’ is simply a newline character without flushing the buffer.
▪ cout is part of the std namespace.
▪ We can either use the full name std::cout or declare using namespace std; to
avoid writing std:: each time.
std (Namespace)
ios (Namespace)
cout, endl
istream (Base class of input stream)
cin
1. Basic structure of cin:
cin << varaibles;
Note:
▪ cin –> Character Input
▪ Defined in the <iostream> header file.
▪ Reads a string without spaces.
▪ The extraction operator (>>) is used to extract values from input streams.
getline(cin, stringVariable);
Note:
▪ Reads a whole line of input, including spaces.
▪ It automatically terminates when it encounters a newline character.
Advanced Usage
1. Reading Characters
cin.get()is used to read a single character, including whitespace.
char ch;
cin.get(ch);
string address;
getline(cin, address);
#include <iostream>
int main() {
int age;
string name;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, name);
// Now getline() reads the full name, not the leftover newline
cout << "Age: " << age << ", Name: " << name << endl;
return 0;
}
4. Skipping Specific Characters
We might want to skip a certain number of characters or up to a specific character.
char ch;
cin.ignore(100, 'x');
5. By default cin.ignore()
cin.ignore() without arguments will skip one character from the input stream. This is
equivalent to calling cin.ignore(1).
#include <iostream>
int main() {
char ch;
return 0;
6. Input Validation
If the user enters data that doesn’t match the type of the variable (e.g., entering a letter when
an int is expected), cin will fail, and the variable will not be modified. To handle such cases,
use cin.fail()
int number;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else {
}
setprecesion
setprecision is a manipulator in C++ that sets the number of digits to be displayed after the
decimal point when outputting floating-point numbers. It is defined in the <iomanip> header
file.
std::cout << std::setprecision(n);
1. Without std::fixed
Sets the total number of significant digits (both before and after the decimal point).
#include <iostream>
int main() {
cout << "Default precision: " << num << endl; // Default precision
cout << "Precision set to 3: " << setprecision(3) << num << endl; // Total
3 significant digits
cout << "Precision set to 5: " << setprecision(5) << num << endl; // Total
5 significant digits
return 0;
Output:
#include <iomanip>
int main() {
cout << "Precision set to 2: " << setprecision(2) << num << endl;
cout << "Precision set to 4: " << setprecision(4) << num << endl;
return 0;
Output:
#include <iomanip>
int main() {
cout << "Precision set to 2: " << setprecision(2) << num << endl;
// 2 digits after the decimal point
cout << "Precision set to 4: " << setprecision(4) << num << endl;
return 0;
Output:
Note:
By default, cout prints floating-point numbers with up to 6 digits of precision. We can change
this using setprecision.
• Basic Structure:
• Syntax:
switch (expression) {
case constant1:
// Code to execute for constant1
break;
case constant2:
// Code to execute for constant2
break;
...
default:
// Code to execute if no case matches
}
• Expression Type:
• The expression in a switch statement must be of integer type (e.g., int, char, enum).
• Cannot use floating-point types (float, double) or non-integer types.
• Case Labels:
• case labels must be compile-time constants and of the same type as the expression.
• Each case label must be unique within the same switch statement.
• break Statement:
• Fall-through Behavior:
• If a case does not end with a break, execution will continue to the next case (even if it
doesn't match).
• Used intentionally to handle multiple cases with shared code.
• Efficiency:
• Limitations:
#include <iostream>
int main() {
int a = 3, b = 7, c = 2, d = 8, e = 5;
std::cout << "The minimum value is: " << minimum << std::endl;
return 0;
#include <initializer_list>
int main() {
return 0;
}
The swap() function is used to exchange the values of two variables. It's a part of the
<algorithm> header and can be used with various data types, including built-in types, user-
defined types, and even standard library containers.
#include <iostream>
int main() {
std::cout << "x = " << x << ", y = " << y << std::endl;
std::cout << "x = " << x << ", y = " << y << std::endl;
return 0;