{"id":1843,"date":"2025-01-25T18:15:51","date_gmt":"2025-01-25T18:15:51","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1843"},"modified":"2025-01-25T20:37:22","modified_gmt":"2025-01-25T20:37:22","slug":"tying-streams","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/i-o-streams\/tying-streams\/","title":{"rendered":"Tying Streams"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Tying_Streams\"><\/span>Tying Streams<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is common practice for interactive programs to prompt for input on one stream (such as <strong><code>cout<\/code><\/strong>) and then wait for input on another stream (such as <strong><code>cin<\/code><\/strong>).<\/p>\n\n\n\n<p>It is possible to <em>tie<\/em> an output stream to an input stream to ensure that any prompt issued on the output stream is displayed before the request for input is made on the input stream<\/p>\n\n\n\n<p>Stream <strong><code>cout<\/code><\/strong> is automatically tied to <strong><code>cin<\/code><\/strong>.&nbsp; However, you may wish to tie two different streams together to cause them to be associated in this way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_tie_Member_Function\"><\/span>The tie Member Function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Use the <strong>tie<\/strong> member function to do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>myInputStream.tie( &amp;myOutputStream );<\/strong><\/code><\/pre>\n\n\n\n<p>It ties the two streams together.<\/p>\n\n\n\n<p>The <strong><code>tie<\/code><\/strong> member function also returns the previously tied stream.&nbsp; There is also a <code><strong>tie<\/strong><\/code> member function that only returns the tied stream:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>myInputStream.tie();<\/strong><\/code><\/pre>\n\n\n\n<p>To untie a stream, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>myInputStream.tie(0); \/\/ pass in 0<\/strong><\/code><\/pre>\n\n\n\n<div style=\"height:47px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/i-o-streams\/output-of-user-defined-types\/\">Next: Output of User-Defined Types<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tying Streams It is common practice for interactive programs to prompt for input on one stream (such as cout) and then wait for input on another stream (such as cin). It is possible to tie an output stream to an input stream to ensure that any prompt issued on the output stream is displayed before [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":936,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1843","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=1843"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1843\/revisions"}],"predecessor-version":[{"id":1860,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1843\/revisions\/1860"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/936"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}