{"id":7764,"date":"2022-09-10T07:37:13","date_gmt":"2022-09-10T02:07:13","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/"},"modified":"2022-09-10T07:37:13","modified_gmt":"2022-09-10T02:07:13","slug":"solved-how-to-input-integer-numbers-with-space-in-between","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/","title":{"rendered":"[Solved] How to input integer numbers with space in between"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-29100839\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"29100839\" data-parentid=\"29100743\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<pre><code>int x;\nwhile(cin&gt;&gt;x)\n{\n  store the number one by one\n}\n\/\/process\n<\/code><\/pre>\n<p>Simply do it this way. Store the numbers in the array.<br \/>\nOr you can do it this way-<\/p>\n<pre><code>string s;\ngetline(cin,s);\nstd::stringstream myss;\nmyss&lt;&lt;s;\nstd::string t;\nint x;\nstd::vector&lt;int&gt; v;\nwhile(std::getline(myss,t,' '))\n{\n    if(std::stringstream(t)&gt;&gt;x)\n    {\n       \/\/ store x in an vector. \n       v.push_back(x);\n    }\n<\/code><\/pre>\n<p>Let&#8217;s elaborate the idea-<\/p>\n<blockquote>\n<p>What is <strong>stream<\/strong>? Informally, a string is a collection of characters, a stream is a tool to manipulate moving data around.<\/p>\n<p>What is <strong>stringstream<\/strong>? A class that operate on strings.Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a <code>string<\/code> object, using member <code>str<\/code>. <\/p>\n<p>How the first code works? <code>cin<\/code> is an object of class <code>istream<\/code> that represents the standard input stream.(from where inputs are fetched) It returns a reference to the same stream for which it is overloaded here <code>istream<\/code>. <code>cin<\/code> is used for formatted string extraction.It&#8217;s possible to make tests like<br \/>\n     if (cin)<br \/>\n       \/\/then do something.<br \/>\n  which will be <code>true<\/code> if <code>cin<\/code> is ok and <code>false<\/code> if an end-of-file or has encountered an error. It can be used easily in case you are reading from a file.<\/p>\n<p>How the second code works? Simply put it this way. Upto the &#8216; &#8216; you are putting a whole line in a <code>string<\/code>. Then you are using the <code>stringstream<\/code> class to get the desired value (here int x). Now when you are extracting from it, you are only extracting a int value. After that it again goes to getline to fetch the next number. As space seperates the numbers each getline is getting a string containing that number. The getline works as follows- <strong>getline(cin,str,delim)<\/strong><br \/>\n  extracts characters from cin and stores them into str until the delimitation character delim is found (or the newline character, &#8216;\\n&#8217; where the delim is not there [means <code>getline(cin,str)<\/code>]),<\/p>\n<p>The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.<br \/>\n  If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.<br \/>\n  Each extracted character is appended to the string.<\/p>\n<p>Now this is how the second code works. Check reference manuals for more deatils.<\/p>\n<\/blockquote>\n<p>Now let&#8217;s see a little bit about <code>vector<\/code>s.<\/p>\n<blockquote>\n<p>vector is a container type in c++. The next question would be probably what is <strong>conatiner<\/strong>?<\/p>\n<p><strong>container<\/strong>: containers are holder objects that store collection of other objects. Simply put, you can keep anything in this containers, be it a int or a object you defined or structure type, anything. This is flexibilty is acieved using template. The template as the name implies simply give us a idea about it&#8217;s work-yes it is a template of any type you can use in place of it.<\/p>\n<p><strong>vector<\/strong>: There are other containers like list,deque etc but vector is quite different from other containers. Vector is quite effiecient where it has to insert or delete element from ends( back end) and access elements randomly- vector provides random access easily like <code>v[i]<\/code> etc. This is the basic of vector. It is better than arrays because it increases dynamically as you push elements. To know how to use it check the reference manual.<\/p>\n<\/blockquote><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">14<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to input integer numbers with space in between <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] int x; while(cin&gt;&gt;x) { store the number one by one } \/\/process Simply do it this way. Store the numbers in the array. Or you can do it this way- string s; getline(cin,s); std::stringstream myss; myss&lt;&lt;s; std::string t; int x; std::vector&lt;int&gt; v; while(std::getline(myss,t,&#8217; &#8216;)) { if(std::stringstream(t)&gt;&gt;x) { \/\/ store x in an vector. v.push_back(x); &#8230; <a title=\"[Solved] How to input integer numbers with space in between\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\" aria-label=\"More on [Solved] How to input integer numbers with space in between\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,1148,702],"class_list":["post-7764","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-input","tag-integer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to input integer numbers with space in between - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to input integer numbers with space in between - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] int x; while(cin&gt;&gt;x) { store the number one by one } \/\/process Simply do it this way. Store the numbers in the array. Or you can do it this way- string s; getline(cin,s); std::stringstream myss; myss&lt;&lt;s; std::string t; int x; std::vector&lt;int&gt; v; while(std::getline(myss,t,&#039; &#039;)) { if(std::stringstream(t)&gt;&gt;x) { \/\/ store x in an vector. v.push_back(x); ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-10T02:07:13+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to input integer numbers with space in between\",\"datePublished\":\"2022-09-10T02:07:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\"},\"wordCount\":531,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"input\",\"integer\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\",\"name\":\"[Solved] How to input integer numbers with space in between - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-10T02:07:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to input integer numbers with space in between\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to input integer numbers with space in between - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to input integer numbers with space in between - JassWeb","og_description":"[ad_1] int x; while(cin&gt;&gt;x) { store the number one by one } \/\/process Simply do it this way. Store the numbers in the array. Or you can do it this way- string s; getline(cin,s); std::stringstream myss; myss&lt;&lt;s; std::string t; int x; std::vector&lt;int&gt; v; while(std::getline(myss,t,' ')) { if(std::stringstream(t)&gt;&gt;x) { \/\/ store x in an vector. v.push_back(x); ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/","og_site_name":"JassWeb","article_published_time":"2022-09-10T02:07:13+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to input integer numbers with space in between","datePublished":"2022-09-10T02:07:13+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/"},"wordCount":531,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","input","integer"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/","name":"[Solved] How to input integer numbers with space in between - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-10T02:07:13+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-input-integer-numbers-with-space-in-between\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to input integer numbers with space in between"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=7764"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7764\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}