logo

Qu'est-ce que l'inclusion d'iostream en C++

Pour effectuer des opérations d'entrée et de sortie en C++, nous devons utiliser les fichiers d'en-tête iostream. Sans fichier d'en-tête, nous ne pouvons pas recevoir les entrées de l'utilisateur ni imprimer de sortie.

Syntaxe du fichier d'en-tête :

 #include #include 'iostream.h' 

Il existe deux types de flux dans le fichier d'en-tête iostream

1. Flux d'entrée : Pour prendre n'importe quelle entrée de l'utilisateur, nous devons utiliser cin, qui appartient au flux d'entrée

Syntaxe pour utiliser le flux d'entrée :

 std::cin>>variable_name 

Lorsque le cin est exécuté, le curseur sera arrêté sur l'instruction particulière jusqu'à ce que la valeur soit saisie. La valeur saisie sera stockée dans une variable.

2. Flux de sortie : Pour imprimer la sortie, nous utilisons des fonctions intégrées dans le flux de sortie cout

Syntaxe pour cout

 std::cout&lt;<variable_name < pre> <p>When cout is executed, the value in the variable will be printed.</p> <h3>Different operations in iostream</h3> <p> <strong>1. Cin</strong> </p> <p>Using cin, we can take input from the user and store the value in the variable. We need to use the cin keyword followed by &gt;&gt; and the variable name.</p> <p> <strong>Syntax:</strong> </p> <pre> std::cin&gt;&gt;variable_name </pre> <p>To use cin, we need to use <strong>#include </strong> as cin belongs to this header file, and without this, an error will occur.</p> <p> <strong>Example code for cin:</strong> </p> <pre> //using header file iostream #include using namespace std; int main() { string name; //cin declaration cin &gt;&gt; name; return 0; } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above code, we used cin to take the input, so to use cin, we included header file. When the input is taken, the string input is stored in the name variable.</p> <p> <strong>2. Cout</strong> </p> <p>To print the output, we need to use the cout keyword, which belongs to the iostream header file. To use cout, we need to use the cout keyword followed by &lt;&lt; and variable or the statement to print the output.</p> <p>The syntax for using cout:</p> <pre> std::cout&lt;<variable_name < pre> <p> <strong>Example of using cout:</strong> </p> <pre> //to use the cout statement, we need to use the iostream header file #include using namespace std; int main() { //cout statement is used here to print the statement cout &lt;&lt; &apos;Hi from cout statement&apos;; return 0;} </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-2.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> :</p> <p>In the above code, we used a cout statement to print the statement. To use the cout statement, we need to include iostream.h header file. Once the cout is executed, statement or variable value will be printing the output. To print any statement, we need to use double quotes (&apos; &apos;), and to print a variable value; we need to use just the variable name without double quotes(&apos; &apos;)</p> <p> <strong>3. Cerr</strong> </p> <p>Cerr is used to print errors in C++, which is present in the iostream header file. If we need to print any error message in the code if any condition fails, then cerr is very helpful.</p> <p>The syntax for cerr is:</p> <pre> cerr&lt;<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin&gt;&gt;a; if(a%2==0){ cout&lt;<'the number entered in even number'<<endl; } else{ std::cerr << 'enter proper number' '
'; return 0; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-3.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above example, we used cin to take the input and check if the given number is even or not. If the given number is not even, we need to print an error that the given number is not an even number. We use the cerr keyword in the iostream header file to print this error.</p> <p> <strong>4. Clog:</strong> </p> <p>The clog is also used to print error messages, but unlike cerr, clog is buffered, which means the error message is stored in a buffer and then will be printed, but cerr is unbuffered and will not store the error message in the buffer. The clog also belongs to the iostream header file. As the clog is buffered, it will not show the error message immediately. The clog is preferred more than cerr when efficiency is more important.</p> <p>The syntax for clog:</p> <pre> clog&lt;<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog &lt;&lt; &apos;This message is stored in the buffer&apos;; return 0; } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-4.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above code, we are printing an error. To print this error, we use cerr, which belongs to the iostream header file. Before printing the error, we store the error in a buffer.</p> <p> <strong>Example</strong> : For all the input and output streams</p> <pre> #include using namespace std; int main() { int a; string b; cin&gt;&gt;a; if(a%2==0){ cout&lt;<'the number entered is even enter a name'<>b; cout&lt; <b; } else{ std::cerr << 'enter even number' '
'; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-5.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> : In the above code, we used all the input and output streams in the iostream.h header file.</p> <hr></b;></'the></pre></variable_name;></pre></'the></pre></variable_name></pre></variable_name></pre></variable_name>

Pour utiliser cin, nous devons utiliser #inclure car cin appartient à ce fichier d'en-tête, et sans cela, une erreur se produira.

Exemple de code pour cin :

 //using header file iostream #include using namespace std; int main() { string name; //cin declaration cin &gt;&gt; name; return 0; } 

Sortir:

Qu'est-ce que l'inclusion d'iostream en C++

Explication:

system.out.println

Dans le code ci-dessus, nous avons utilisé cin pour prendre l'entrée, donc pour utiliser cin, nous avons inclus le fichier d'en-tête. Lorsque l'entrée est prise, la chaîne d'entrée est stockée dans la variable name.

2. COUTURE

tri à bulles dans l'algorithme

Pour imprimer la sortie, nous devons utiliser le mot-clé cout, qui appartient au fichier d'en-tête iostream. Pour utiliser cout, nous devons utiliser le mot-clé cout suivi de << et d'une variable ou de l'instruction pour imprimer la sortie.

La syntaxe d'utilisation de cout :

 std::cout&lt;<variable_name < pre> <p> <strong>Example of using cout:</strong> </p> <pre> //to use the cout statement, we need to use the iostream header file #include using namespace std; int main() { //cout statement is used here to print the statement cout &lt;&lt; &apos;Hi from cout statement&apos;; return 0;} </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-2.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> :</p> <p>In the above code, we used a cout statement to print the statement. To use the cout statement, we need to include iostream.h header file. Once the cout is executed, statement or variable value will be printing the output. To print any statement, we need to use double quotes (&apos; &apos;), and to print a variable value; we need to use just the variable name without double quotes(&apos; &apos;)</p> <p> <strong>3. Cerr</strong> </p> <p>Cerr is used to print errors in C++, which is present in the iostream header file. If we need to print any error message in the code if any condition fails, then cerr is very helpful.</p> <p>The syntax for cerr is:</p> <pre> cerr&lt;<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin&gt;&gt;a; if(a%2==0){ cout&lt;<\'the number entered in even number\'<<endl; } else{ std::cerr << \'enter proper number\' \'
\'; return 0; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-3.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above example, we used cin to take the input and check if the given number is even or not. If the given number is not even, we need to print an error that the given number is not an even number. We use the cerr keyword in the iostream header file to print this error.</p> <p> <strong>4. Clog:</strong> </p> <p>The clog is also used to print error messages, but unlike cerr, clog is buffered, which means the error message is stored in a buffer and then will be printed, but cerr is unbuffered and will not store the error message in the buffer. The clog also belongs to the iostream header file. As the clog is buffered, it will not show the error message immediately. The clog is preferred more than cerr when efficiency is more important.</p> <p>The syntax for clog:</p> <pre> clog&lt;<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog &lt;&lt; &apos;This message is stored in the buffer&apos;; return 0; } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-4.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above code, we are printing an error. To print this error, we use cerr, which belongs to the iostream header file. Before printing the error, we store the error in a buffer.</p> <p> <strong>Example</strong> : For all the input and output streams</p> <pre> #include using namespace std; int main() { int a; string b; cin&gt;&gt;a; if(a%2==0){ cout&lt;<\'the number entered is even enter a name\'<>b; cout&lt; <b; } else{ std::cerr << \'enter even number\' \'
\'; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-5.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> : In the above code, we used all the input and output streams in the iostream.h header file.</p> <hr></b;></\'the></pre></variable_name;></pre></\'the></pre></variable_name></pre></variable_name>

Sortir:

Qu'est-ce que l'inclusion d'iostream en C++

Explication :

Dans le code ci-dessus, nous avons utilisé une instruction cout pour imprimer l'instruction. Pour utiliser l'instruction cout, nous devons inclure le fichier d'en-tête iostream.h. Une fois le cout exécuté, l'instruction ou la valeur de la variable imprimera la sortie. Pour imprimer n'importe quelle instruction, nous devons utiliser des guillemets doubles (' ') et imprimer une valeur variable ; nous devons utiliser uniquement le nom de la variable sans guillemets doubles (' ')

3. Cerr

Cerr est utilisé pour imprimer les erreurs en C++, qui sont présentes dans le fichier d'en-tête iostream. Si nous devons imprimer un message d'erreur dans le code si une condition échoue, alors cerr est très utile.

La syntaxe de cerr est :

 cerr&lt;<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin&gt;&gt;a; if(a%2==0){ cout&lt;<\'the number entered in even number\'<<endl; } else{ std::cerr << \'enter proper number\' \'
\'; return 0; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-3.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above example, we used cin to take the input and check if the given number is even or not. If the given number is not even, we need to print an error that the given number is not an even number. We use the cerr keyword in the iostream header file to print this error.</p> <p> <strong>4. Clog:</strong> </p> <p>The clog is also used to print error messages, but unlike cerr, clog is buffered, which means the error message is stored in a buffer and then will be printed, but cerr is unbuffered and will not store the error message in the buffer. The clog also belongs to the iostream header file. As the clog is buffered, it will not show the error message immediately. The clog is preferred more than cerr when efficiency is more important.</p> <p>The syntax for clog:</p> <pre> clog&lt;<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog &lt;&lt; &apos;This message is stored in the buffer&apos;; return 0; } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-4.webp" alt="What is include iostream in C++"> <p> <strong>Explanation:</strong> </p> <p>In the above code, we are printing an error. To print this error, we use cerr, which belongs to the iostream header file. Before printing the error, we store the error in a buffer.</p> <p> <strong>Example</strong> : For all the input and output streams</p> <pre> #include using namespace std; int main() { int a; string b; cin&gt;&gt;a; if(a%2==0){ cout&lt;<\'the number entered is even enter a name\'<>b; cout&lt; <b; } else{ std::cerr << \'enter even number\' \'
\'; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-5.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> : In the above code, we used all the input and output streams in the iostream.h header file.</p> <hr></b;></\'the></pre></variable_name;></pre></\'the></pre></variable_name>

Sortir:

Qu'est-ce que l'inclusion d'iostream en C++

Explication:

Dans le code ci-dessus, nous imprimons une erreur. Pour imprimer cette erreur, nous utilisons cerr, qui appartient au fichier d'en-tête iostream. Avant d'imprimer l'erreur, nous stockons l'erreur dans un tampon.

Exemple : Pour tous les flux d'entrée et de sortie

 #include using namespace std; int main() { int a; string b; cin&gt;&gt;a; if(a%2==0){ cout&lt;<\\'the number entered is even enter a name\\'<>b; cout&lt; <b; } else{ std::cerr << \\'enter even number\\' \\'
\\'; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/23/what-is-include-iostream-c-5.webp" alt="What is include iostream in C++"> <p> <strong>Explanation</strong> : In the above code, we used all the input and output streams in the iostream.h header file.</p> <hr></b;></\\'the>