Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying a vector of strings in C++
    text
    copied!<p>I'm sorry if this is a repeat question but I already tried to search for an answer and came up empty handed. So basically I just want to add strings (single words) to the back of a vector and then display the stored strings as a single string. I am quite the rookie.</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;cctype&gt; using namespace std; int main(int a, char* b []) { vector&lt;string&gt; userString; string word; string sentence = ""; for (decltype(userString.size()) i = 0; i &lt;= userString.size() - 1; i++) { cin &gt;&gt; word; userString.push_back(word); sentence += userString[i] + " "; } cout &lt;&lt; sentence; system("PAUSE"); return 0; } </code></pre> <p>Why doesn't this work?</p> <p><strong>EDIT</strong></p> <pre><code>int main(int a, char* b []) { cout &lt;&lt; "Enter a sequence of words. Enter '.' \n"; vector&lt;string&gt; userString; string word; string sentence = ""; / int wordCount = 0; while (getline(cin, word)) { if (word == ".") { break; } userString.push_back(word); } for (decltype(userString.size()) i = 0; i &lt;= userString.size() - 1; i++) { sentence += userString[i] + " "; wordCount += 1; if (wordCount == 8) { sentence = sentence + "\n"; wordCount = 0; } } cout &lt;&lt; sentence &lt;&lt; endl; system("PAUSE"); return 0; } </code></pre> <p>So my new program works. It just puts values at the back of a vector and prints them out 8 words to a line. I know there's easier ways but I'm just learning vectors and I'm going in baby steps. Thanks for the help guys.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload