title: getline() error in C++ when using for loop
body: <p>I had to write a program to calculate average speed of a car during a trip and I have to prompt the user for the names of the two cities that they were traveling between. That program worked, but the next program we had to write is an addition to the last one, in which the user is prompted for how many times the program should run. I declared a new variable for the number of times the users needs the program to run. However when the program enters the loop it skips the first getline() method (asking for the origin city) and skips directly to the second getline() (method asking for the destination city). I have tried clearing the buffer and declaring the loop differently, but whatever I do the string is still read into the program as an empty string. Just wondering if I made a mistake on something or if I cannot use getline() in this instance.</p>
  
  <p>Using C++ and Codeblocks IDE with the GNU Compiler (I've tried other compilers too)</p>
  
  <p>Anyhow here is the code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  int main()
  {
      //Declare Variables
      string orgcity = "";
      string destcity = "";
      int hours = 0;
      double minutes = 0.0;
      int hoursmin = 0;
      int minutesmin = 0;
      int minutesmax = 60;
      double dist = 0.0;
      double totalhours = 0.0;
      double avespeed = 0.0;
      double num1 = 0;
  
      cout &lt;&lt; "How many times would you like to calculate the average speed: ";
      cin &gt;&gt; num1;
  
      for(num1; num1 &gt; 0; --num1)
      {
  
      //Collect Data
      cout &lt;&lt; "Enter the city of origin: ";
      getline(cin, orgcity);
  
      cout &lt;&lt; "Enter the destination city: ";
      getline(cin, destcity);
  
      //If Statements and Loops...Start here
      do {
          cout &lt;&lt; "Enter the number of hours spent in travel: ";
          cin &gt;&gt; hours;
  
          if (hours &lt; hoursmin)
              cout &lt;&lt; "     Invalid number of hours - must be &gt;= 0" &lt;&lt; endl;
  
      } while (hours &lt; hoursmin);
  
      do {
      cout &lt;&lt; "Enter the number of minutes spent in travel: ";
      cin &gt;&gt; minutes;
  
      if (minutes &gt;= minutesmax){
      cout &lt;&lt;"     Invalid number of minutes - must be in range 0..59" &lt;&lt; endl;
      }
      if (minutes &lt;= minutesmin) {
          cout &lt;&lt; "     Invalid number of minutes - must be in range 0..59" &lt;&lt; endl;
      }
      } while (minutes &gt;= minutesmax);
  
      //End Here
  
      cout &lt;&lt; "Enter the distance (in miles) between the two cities: ";
      cin &gt;&gt; dist;
      cout &lt;&lt; endl;
  
      //Formula and Final Prompt
      totalhours = (hours + (minutes / 60.0));
      avespeed = dist / totalhours;
  
      cout &lt;&lt; "The average speed of the vehicle traveling" &lt;&lt; endl;
      cout &lt;&lt; "between " &lt;&lt; orgcity &lt;&lt; " and " &lt;&lt; destcity &lt;&lt; " is " &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; avespeed &lt;&lt; " miles per hour." &lt;&lt; endl;
      cout &lt;&lt; "-------------------------------------------------------------------------------" &lt;&lt; endl;
      }
      return 0;
  
  }
  </code></pre>