Note that there are some explanatory texts on larger screens.

plurals
  1. POString Vector program exits before input
    text
    copied!<p>So, I have a project that must add, delete, and print the contents of a vector... the problem is that, when run the program exits before I can type in the string to add to the vector. I commented the function that that portion is in.</p> <p>Thanks!</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; #include &lt;vector&gt; #include &lt;string&gt; using namespace std; void menu(); void addvector(vector&lt;string&gt;&amp; vec); void subvector(vector&lt;string&gt;&amp; vec); void vectorsize(const vector&lt;string&gt;&amp; vec); void printvec(const vector&lt;string&gt;&amp; vec); void printvec_bw(const vector&lt;string&gt;&amp; vec); int main() { vector&lt;string&gt; svector; menu(); return 0; } //functions definitions void menu() { vector&lt;string&gt; svector; int choice = 0; cout &lt;&lt; "Thanks for using this program! \n" &lt;&lt; "Enter 1 to add a string to the vector \n" &lt;&lt; "Enter 2 to remove the last string from the vector \n" &lt;&lt; "Enter 3 to print the vector size \n" &lt;&lt; "Enter 4 to print the contents of the vector \n" &lt;&lt; "Enter 5 ----------------------------------- backwards \n" &lt;&lt; "Enter 6 to end the program \n"; cin &gt;&gt; choice; switch(choice) { case 1: addvector(svector); break; case 2: subvector(svector); break; case 3: vectorsize(svector); break; case 4: printvec(svector); break; case 5: printvec_bw(svector); break; case 6: exit(1); default: cout &lt;&lt; "not a valid choice \n"; // menu is structured so that all other functions are called from it. } } void addvector(vector&lt;string&gt;&amp; vec) { string line; int i = 0; cout &lt;&lt; "Enter the string please \n"; getline(cin, line); // doesn't prompt for input! vec.push_back(line); } void subvector(vector&lt;string&gt;&amp; vec) { vec.pop_back(); return; } void vectorsize(const vector&lt;string&gt;&amp; vec) { if (vec.empty()) { cout &lt;&lt; "vector is empty"; } else { cout &lt;&lt; vec.size() &lt;&lt; endl; } return; } void printvec(const vector&lt;string&gt;&amp; vec) { for(int i = 0; i &lt; vec.size(); i++) { cout &lt;&lt; vec[i] &lt;&lt; endl; } return; } void printvec_bw(const vector&lt;string&gt;&amp; vec) { for(int i = vec.size(); i &gt; 0; i--) { cout &lt;&lt; vec[i] &lt;&lt; endl; } return; } </code></pre>
 

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