Better way to repeatedly use istringstream in "counting minutes" challenge I was doing this easy challenge "Counting Minutes I" from Coderbyte.com where you just calculate the number of minutes between two given times. 

The second time is always after the first, although it may look to be before but mean it's during the next day. 

The format of their test cases is "hour1:minute1-hour2:minute2" such as "12:30am-12:00pm" where the difference would be 11.5 hours = 1425 minutes.

In converting different parts of the string containing the times to ints I've been using istringstream but creating a new one each time I call the function to set the ints. It seems like this is a sloppy way of accomplishing this, is there a better way like creating one istringstream in my main CountingMinutesI function and passing that as an argument to getNum then clearing it out after setting each particular int? Or is this also a bad idea?

#include &lt;iostream&gt;
#include &lt;sstream&gt;
using namespace std;

void getNum (string timeString, int&amp; pos, int&amp; result) {
  string temp;
  while (isdigit(timeString[pos])) {
    temp += timeString[pos];
    pos++;
  }
  istringstream iss (temp);
  iss &gt;&gt; result;
}

void to24HourTime(string timeString, int&amp; pos, int&amp; hourToConvert) {
  if (timeString[pos] == 'p' &amp;&amp; hourToConvert != 12)
    hourToConvert += 12;
  if (timeString[pos] == 'a' &amp;&amp; hourToConvert == 12)
    hourToConvert = 0;
}

int CountingMinutesI(string str) { 

  int hour1int, hour2int, min1int, min2int, minDifference;
  int stringPos = 0;

  getNum(str, stringPos, hour1int);
  stringPos++;
  getNum(str, stringPos, min1int);

  to24HourTime(str, stringPos, hour1int);

  stringPos += 3;

  getNum(str, stringPos, hour2int);
  stringPos++;
  getNum(str, stringPos, min2int);

  to24HourTime(str, stringPos, hour2int);

  if (hour2int &lt; hour1int) //second time is the during the next day
    hour2int += 24;
  if (hour1int == hour2int &amp;&amp; min1int &gt; min2int) //time difference would look negative, but is really between 23 and 24 hours
    hour2int += 24;
  if (min2int &lt; min1int) { //simplify subtracting minutes to add to total difference
    min2int += 60;
    hour2int--;
  }

  minDifference = (hour2int - hour1int) * 60 + (min2int - min1int);

  return minDifference;

}

int main() { 

  // keep this function call here
  cout &lt;&lt; CountingMinutesI(gets(stdin));
  return 0;

}
In to24HourTime(), pos doesn't need to be passed by reference as it's not being modified.  Just pass it by value.

Instead of passing hourToConvert by reference (which is being modified), you can just pass by value and then return the result.  It may also increase readability as it'll be clear that the third argument is to be reassigned by the function.

Final changes:

hour1int = to24HourTime(str, stringPos, hour1int);

// ...

int to24HourTime(string timeString, int pos, int hourToConvert) {
    if (timeString[pos] == 'p' &amp;&amp; hourToConvert != 12)
        hourToConvert += 12;
    if (timeString[pos] == 'a' &amp;&amp; hourToConvert == 12)
        hourToConvert = 0;

    return hourToConvert;
}