K provádění jakýchkoliv vstupních a výstupních operací v C++ musíme použít hlavičkové soubory iostream. Bez souboru záhlaví nemůžeme převzít vstup od uživatele ani vytisknout žádný výstup.
Syntaxe hlavičkového souboru:
#include #include 'iostream.h'
V záhlaví souboru iostream jsou dva typy proudů
1. Vstupní proud : Abychom převzali jakýkoli vstup od uživatele, musíme použít cin, který patří do vstupního proudu
Syntaxe pro použití vstupního proudu:
if else v java
std::cin>>variable_name
Po provedení cin se kurzor zastaví na konkrétním příkazu, dokud nezadáte hodnotu. Zadaná hodnota bude uložena do proměnné.
2. Výstupní proud : Pro tisk výstupu používáme vestavěné funkce ve výstupním proudu cout
Syntaxe pro cout
std::cout<<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 >> and the variable name.</p> <p> <strong>Syntax:</strong> </p> <pre> std::cin>>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 >> 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 << and variable or the statement to print the output.</p> <p>The syntax for using cout:</p> <pre> std::cout<<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 << 'Hi from cout statement'; 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 (' '), and to print a variable value; we need to use just the variable name without double quotes(' ')</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<<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin>>a; if(a%2==0){ cout<<'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<<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog << 'This message is stored in the buffer'; 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>>a; if(a%2==0){ cout<<'the number entered is even enter a name'<>b; cout< <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>
Abychom mohli použít cin, musíme použít #zahrnout protože cin patří do tohoto hlavičkového souboru a bez něj dojde k chybě.
Příklad kódu pro cin:
kdy vyšel win 7
//using header file iostream #include using namespace std; int main() { string name; //cin declaration cin >> name; return 0; }
Výstup:
Vysvětlení:
výjimka java throw
Ve výše uvedeném kódu jsme použili cin k převzetí vstupu, takže pro použití cin jsme zahrnuli hlavičkový soubor. Když je vstup převzat, vstup řetězce se uloží do proměnné name.
2. Cout
Pro tisk výstupu musíme použít klíčové slovo cout, které patří do hlavičkového souboru iostream. Chcete-li použít cout, musíme použít klíčové slovo cout následované << a proměnnou nebo příkazem pro tisk výstupu.
Syntaxe pro použití cout:
std::cout<<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 << 'Hi from cout statement'; 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 (' '), and to print a variable value; we need to use just the variable name without double quotes(' ')</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<<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin>>a; if(a%2==0){ cout<<\'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<<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog << 'This message is stored in the buffer'; 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>>a; if(a%2==0){ cout<<\'the number entered is even enter a name\'<>b; cout< <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>
Výstup:
Vysvětlení :
Ve výše uvedeném kódu jsme k vytištění příkazu použili příkaz cout. Chcete-li použít příkaz cout, musíme zahrnout hlavičkový soubor iostream.h. Jakmile je cout proveden, příkaz nebo hodnota proměnné vytiskne výstup. Chcete-li vytisknout jakýkoli příkaz, musíme použít dvojité uvozovky (' ') a vytisknout hodnotu proměnné; musíme použít pouze název proměnné bez uvozovek (' ')
připojení řetězce v jazyce Java
3. Cerr
Cerr se používá k tisku chyb v C++, který je přítomen v záhlaví souboru iostream. Pokud potřebujeme vytisknout jakoukoli chybovou zprávu v kódu, pokud některá podmínka selže, pak je cerr velmi užitečný.
Syntaxe pro cerr je:
cerr<<variable_name < pre> <p>Example:</p> <pre> #include using namespace std; int main() { int a; cin>>a; if(a%2==0){ cout<<\'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<<variable_name; < pre> <p> <strong>Example:</strong> </p> <pre> #include using namespace std; int main() { clog << 'This message is stored in the buffer'; 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>>a; if(a%2==0){ cout<<\'the number entered is even enter a name\'<>b; cout< <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>
Výstup:
herec ranbir kapoor věk
Vysvětlení:
Ve výše uvedeném kódu tiskneme chybu. K vytištění této chyby používáme cerr, který patří do hlavičkového souboru iostream. Před tiskem chyby uložíme chybu do vyrovnávací paměti.
Příklad : Pro všechny vstupní a výstupní toky
#include using namespace std; int main() { int a; string b; cin>>a; if(a%2==0){ cout<<\\'the number entered is even enter a name\\'<>b; cout< <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>\'the>