{"id":2489,"date":"2016-01-27T13:03:44","date_gmt":"2016-01-27T13:03:44","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2489"},"modified":"2016-01-27T13:03:44","modified_gmt":"2016-01-27T13:03:44","slug":"keyboard-input","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/console\/keyboard-input","title":{"rendered":"Keyboard Input"},"content":{"rendered":"<h4>\nGetting input from the keyboard via the command line<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;sstream&gt;\r\nusing namespace std;\r\n\t\r\n\tstring entered_string;\r\n\tfloat my_float=0;\r\n\tint my_int=0;\r\n\r\n\t\/\/Get input\r\n\tcout &lt;&lt; &quot;Enter something: &quot;;\r\n\tgetline(cin, entered_string);\r\n\r\n\t\/\/Convert and display it\r\n\tstringstream(entered_string) &gt;&gt; my_float;\t\t\/\/Will be 0 if invalid\r\n\tstringstream(entered_string) &gt;&gt; my_int;\t\t\t\/\/Will be 0 if invalid\r\n\r\n\tif (entered_string.compare(&quot;4&quot;) == 0)\t\t\/\/0=strings are the same\r\n\t{\r\n\t\tcout &lt;&lt; &quot;It matched!&quot; &lt;&lt; endl;\r\n\t}\r\n  \r\n\tcout &lt;&lt; &quot;You entered: &quot; &lt;&lt; entered_string &lt;&lt; &quot;.  As a float: &quot; &lt;&lt; my_float &lt;&lt; &quot;.  As an int: &quot; &lt;&lt; my_int &lt;&lt; endl;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting input from the keyboard via the command line #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;sstream&gt; using namespace std; string entered_string; float my_float=0; int my_int=0; \/\/Get input cout &lt;&lt; &quot;Enter something: &quot;; getline(cin, entered_string); \/\/Convert and display it stringstream(entered_string) &gt;&gt; my_float; \/\/Will be 0 if invalid stringstream(entered_string) &gt;&gt; my_int; \/\/Will be 0 if invalid if (entered_string.compare(&quot;4&quot;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-2489","post","type-post","status-publish","format-standard","hentry","category-console"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2489"}],"version-history":[{"count":1,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2489\/revisions"}],"predecessor-version":[{"id":2490,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2489\/revisions\/2490"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}