{"id":170,"date":"2020-08-29T19:04:50","date_gmt":"2020-08-29T19:04:50","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=170"},"modified":"2020-08-29T19:04:50","modified_gmt":"2020-08-29T19:04:50","slug":"c-input-and-output","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-input-and-output\/","title":{"rendered":"C++ Input and Output"},"content":{"rendered":"<p>IN this section, we would learn about C++ inputs and output. This is very important in writing interactive programs that accept input from user and sent output to user as well.<\/p>\r\n<p>The two objects use to accomplish this are: cout and cin.<\/p>\r\n<ol>\r\n<li><a href=\"#t1\">Output Using cout<\/a><\/li>\r\n<li><a href=\"#t2\">Input Using cin<\/a><\/li>\r\n<li><a href=\"#t3\">Example &#8211; Calculate Sum<\/a><\/li>\r\n<\/ol>\r\n<p>&nbsp;<\/p>\r\n<h4><strong id=\"t1\">1. Output Using cout<\/strong><\/h4>\r\n<p>You use cout to display formatted output to the standart output device such as the monitor screen. Cout is an instance of the ostream class. Therefore, you need to include &lt;iostream&gt;. You use cout along with the stream insertion operator &lt;&lt;. Let&#8217;s take a simple example.<\/p>\r\n<p><!-- HTML generated using hilite.me --><\/p>\r\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome to C++!\"<\/span>;\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\r\n<p>The above code uses cout to display the message &#8216;Welcome to C++!&#8217; to the screen.<\/p>\r\n<p>The &lt;iostream&gt; file contains defintion for the following objects<\/p>\r\n<ul>\r\n<li>cin &#8211; standard input stream<\/li>\r\n<li>cout &#8211; standard output stream<\/li>\r\n<li>cerr &#8211; unbuffered error stream<\/li>\r\n<li>clog &#8211; buffered error stream<\/li>\r\n<\/ul>\r\n<h4>\u00a0<\/h4>\r\n<h4><strong  id=\"t2\">2. Input Using cin<\/strong><\/h4>\r\n\r\n<p>The cin object is an instance of the istream class and is used to read user input from standard input, for instance, the keyboard. You use it with the stream extraction operator &gt;&gt;. The code below prompts the user for his name. The it gets his name as input and displays a greeting using the name input.<\/p>\r\n<!-- HTML generated using hilite.me -->\r\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\tstring name;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Please enter your name: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> name;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Good day, \"<\/span> <span style=\"color: #333333;\">+<\/span> name;\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\r\n<p>The output is shown below:<\/p>\r\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/C-Input-and-Output.jpg\"><br \/><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-174 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/C-Input-and-Output.jpg\" alt=\"C++ Input and Output\" width=\"411\" height=\"145\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/C-Input-and-Output.jpg 411w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/C-Input-and-Output-300x106.jpg 300w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/a><\/p>\r\n<p>What happens is that when the compiler encounters the <em>cin&gt;&gt;<\/em> statement, I pauses for user input. So when user enters a value and hits Enter, then the value is assigned to the variable, <em>name<\/em>.<\/p>\r\n<p>&nbsp;<\/p>\r\n<h4><strong id=\"t3\">3. Example &#8211; Calculate Sum<\/strong><\/h4>\r\n<p>We could also us cout and cin with numbers. Let&#8217;s illustrate this using a simple calculator program. The program would prompt the user for a first number. Then prompt for a second number. Finally, it would display the sum of the two numbers.<\/p>\r\n<!-- HTML generated using hilite.me -->\r\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> num1;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> num2;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter the first number: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> num1;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter the second number: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> num2;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"The sum is \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> num1 <span style=\"color: #333333;\">+<\/span> num2;\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\r\n<p>Note that the extraction operation have been used twice in the same like. This is completely acceptable.<\/p>\r\n<p>But you may wonder what happens if the user enters a character, say, &#8216;A&#8217; instead of a number. Well, there would be an error. In subsequent lessons, we&#8217;ll see how to handle this.<\/p>\r\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>IN this section, we would learn about C++ inputs and output. This is very important in writing interactive programs that accept input from user and &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/170\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}