title: Ignore Spaces Using getline in C++
body: <p>Hey, I'm trying to write a program that will accept new tasks from people, add it to a stack, be able to display the task, be able to save that stack to a text file, and then read the text file. The issue comes when I am trying to accept input from the user, whenever you enter a string with a space in it, the menu to choose what to do just loops. I need a way to fix this. Any help would be greatly appreciated.</p>
  
  <pre><code>// basic file io operations
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stack&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main () {
      //Declare the stack
      stack&lt;string&gt; list;
  
      //Begin the loop for the menu
      string inputLine;
      cout &lt;&lt; "Welcome to the to-do list!" &lt;&lt; endl;
  
      //Trying to read the file
      ifstream myfile ("to-do.txt");
      if(myfile.is_open()){
  
          //read every line of the to-do list and add it to the stack
          while(myfile.good()){
              getline(myfile,inputLine);
              list.push(inputLine);
          }
          myfile.close();
          cout &lt;&lt; "File read successfully!" &lt;&lt; endl;
      } else {
          cout &lt;&lt; "There was no file to load... creating a blank stack." &lt;&lt; endl;
      }
  
      int option;
  
      //while we dont want to quit
      while(true){
          //display the options for the program
          cout &lt;&lt; endl &lt;&lt; "What would you like to do?" &lt;&lt; endl;
          cout &lt;&lt; "1. View the current tasks on the stack." &lt;&lt; endl;
          cout &lt;&lt; "2. Remove the top task in the stack." &lt;&lt; endl;
          cout &lt;&lt; "3. Add a new task to the stack." &lt;&lt; endl;
          cout &lt;&lt; "4. Save the current task to a file." &lt;&lt; endl;
          cout &lt;&lt; "5. Exit." &lt;&lt; endl &lt;&lt; endl;
  
          //get the input from the user
          cin &gt;&gt; option;
  
          //use the option to do the necessary task
          if(option &lt; 6 &amp;&amp; option &gt; 0){
              if(option == 1){
                  //create a buffer list to display all
                  stack&lt;string&gt; buff = list;
                  cout &lt;&lt; endl;
                  //print out the stack
                  while(!buff.empty()){
                      cout &lt;&lt; buff.top() &lt;&lt; endl;
                      buff.pop();
                  }
              }else if (option == 2){
                  list.pop();
              }else if (option == 3){
                  //make a string to hold the input
                  string task;
                  cout &lt;&lt; endl &lt;&lt; "Enter the task that you would like to add:" &lt;&lt; endl;
                  getline(cin, task); // THIS IS WHERE THE ISSUE COMES IN
                  cin.ignore();
  
                  //add the string
                  list.push(task);
                  cout &lt;&lt; endl;
              }else if (option == 4){
                  //write the stack to the file
                  stack&lt;string&gt; buff = list;
                  ofstream myfile ("to-do.txt");
                  if (myfile.is_open()){
                      while(!buff.empty()){
                          myfile &lt;&lt; buff.top();
                          buff.pop();
                          if(!buff.empty()){
                              myfile &lt;&lt; endl;
                          }
                      }
                  }
                  myfile.close();
              }else{
                  cout &lt;&lt; "Thank you! And Goodbye!" &lt;&lt; endl;
                  break;
              }
          } else {
              cout &lt;&lt; "Enter a proper number!" &lt;&lt; endl;
          }
      }
  }
  </code></pre>