title: Reading string with spaces in c++
body: <p>How can I read input line(type string) with whitespace? I tried getline but it goes into infinite loop. Following is my code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  
  #define MAX 50 //size of array
  
  //Used G++ 4.6.3 compiler
  using namespace std;
  
  int main() {
  
  struct Manager {
  string name;
  int age;
  int working_years;
  string phone;
  int salary;
  }info[MAX];
  
  char inp; //To choose options
  int array_pos = 0; //Current position in array of Manager structure
  string search_name; //Manager name you want to search
  
  cout &lt;&lt; "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
  cin &gt;&gt; inp;
  
  while(inp != 'a') {
  int search_num = 0; //array position at which search result is found
  int found = 0;
  if (inp == 'i' || inp == 's') {
      if (inp == 'i') {
          int k = array_pos;
          cout &lt;&lt; "Enter the information of the manager no "&lt;&lt;k+1&lt;&lt;" is : "; 
  
          cout &lt;&lt; "Enter the Name : "; 
                       //infinte loop occurs
          getline(info[array_pos].name, '\n');
          //cin &gt;&gt; info[array_pos].name;
  
          cout&lt;&lt;"Enter manager age : "; 
          cin &gt;&gt; info[array_pos].age;
  
          cout &lt;&lt; "Enter manage working years : ";
          cin &gt;&gt; info[array_pos].working_years;
  
          cout &lt;&lt; "Enter manager phone no. : ";
          cin &gt;&gt; info[array_pos].phone;
  
          cout &lt;&lt; "Enter manager salary : ";
          cin &gt;&gt; info[array_pos].salary;
          array_pos++;
      }
      if (inp == 's') {
          cout &lt;&lt; "Enter the manager name you want to search : ";
          cin &gt;&gt; search_name;
          for(int i = 0; i &lt; array_pos; i++) {
              //using str1.compare(str2) to compare manager name
              if(info[i].name.compare(search_name) == 0) { //manager name found in array of structure
                  found = 1;                  
                  search_num = i;                 
                  cout &lt;&lt; "Name : " &lt;&lt; info[search_num].name &lt;&lt; "\n";
                  cout &lt;&lt; "Age: " &lt;&lt; info[search_num].age &lt;&lt; "\n";
                  cout &lt;&lt; "Working Years: " &lt;&lt; info[search_num].working_years &lt;&lt; "\n";
                  cout &lt;&lt; "Phone No. : " &lt;&lt; info[search_num].phone &lt;&lt; "\n";
                  cout &lt;&lt; "Salary : " &lt;&lt; info[search_num].salary &lt;&lt; "\n";
              } //end of if loop for comparing string
          } //end of for loop for searching
          if(found == 0)
              cout &lt;&lt; "No Manager by this name exist in record" &lt;&lt; "\n"; 
  
      } //end of if loop
  
  } //end of if loop for  searching or insertion
  if(inp == 'a')
      break;
  
  cout &lt;&lt; "Press 'i' to insert manager information or 's' to search for manager information by name or 'a' to abort: ";
  cin &gt;&gt; inp;
  } //end of while loop
  
  return 0;
  }
  </code></pre>