HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Better way to repeatedly use istringstream in "counting minutes" challenge

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
istringstreamcountingchallengewaybetterminutesuserepeatedly

Problem

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
#include
using namespace std;

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

void to24HourTime(string timeString, int& pos, int& hourToConvert) {
if (timeString[pos] == 'p' && hourToConvert != 12)
hourToConvert += 12;
if (timeString[pos] == 'a' && 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 min2int) //time difference would look negative, but is really between 23 and 24 hours
hour2int += 24;
if (min2int < min1int) { //simplify subtracting minutes to add to total difference
min2int += 60;
hour2int--;
}

mi

Solution

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' && hourToConvert != 12)
        hourToConvert += 12;
    if (timeString[pos] == 'a' && hourToConvert == 12)
        hourToConvert = 0;

    return hourToConvert;
}

Code Snippets

hour1int = to24HourTime(str, stringPos, hour1int);

// ...

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

    return hourToConvert;
}

Context

StackExchange Code Review Q#48284, answer score: 5

Revisions (0)

No revisions yet.