Hi, hope someone can help with an issue I'm having.

Basically, I've got an input file containing, amongst other things, an int on each line representing a time stamp in the form of hhmmss. I needed to make a comparison from line to line to make sure that no more than 3 minutes goes between each set of measurements.

I tried setting it up so that the int was passed to a stringstream, converted to a string, then the substring for two digits at a time were passed to a different stringstream, then converted back to separate ints. The problem with this solution is that the time stamp is in military time, so it's potentially got multiple leading zeroes, which will be lost in the conversion process from int to string to int.

Is there a similar function to substr for an int? Or am I going about this the entirely wrong way? Any advice would be welcome.

Here's the currently non-operational snippet.

stringstream stampin, hoursin, minutesin, secondsin;
                string temp, tempa[3];
                int times[3];
                stampin << numbers[2];
                stampin >> temp;
                tempa[0] = temp.substr(0,2);
                tempa[1] = temp.substr(2,2);
                tempa[2] = temp.substr(4,2);
                hoursin << tempa[0];
                minutesin << tempa[1];
                secondsin << tempa[2];
                hoursin >> times[0];
                minutesin >> times[1];
                secondsin >> times[2];

Recommended Answers

All 2 Replies

>>so it's potentially got multiple leading zeroes, which will be lost in the conversion process from int to string to int.

What's the problem with that? Integers never contain leading zeros, whether the strings have them or not.

What I would do is convert the three integers into seconds so that they can be easily compared with current time. int totalSeconds = hours * 60 * 60 + minutes* 60 + seconds; Now get a struct* tm for current and make the same calculation.

I actually realized that the initial capture of the first in was the problem. It sometimes had leading zeroes, and so could potentially throw an error when using the substr function as I had it set up. For future reference to future readers:

stringstream stampin, hoursin, minutesin, secondsin;
                string temp, tempa[3];
                int times[3];
                stampin << numbers[2];
                stampin >> temp;
                if(numbers[2] < 10)
                {
                    tempa[2] = temp.substr(0,1);
                    secondsin << tempa[2];
                    times[0] = 0;
                    times[1] = 0;
                    secondsin >> times[2];
                }
                else if(numbers[2] < 100)
                {
                    tempa[2] = temp.substr(0,2);
                    secondsin << tempa[2];
                    times[0] = 0;
                    times[1] = 0;
                    secondsin >> times[2];
                }
                else if(numbers[2] < 1000)
                {
                    tempa[1] = temp.substr(0,1);
                    tempa[2] = temp.substr(1,2);
                    minutesin << tempa[1];
                    secondsin << tempa[2];
                    times[0] = 0;
                    minutesin >> times[1];
                    secondsin >> times[2];
                }
                else if(numbers[2] < 10000)
                {
                    tempa[1] = temp.substr(0,2);
                    tempa[2] = temp.substr(2,2);
                    minutesin << tempa[1];
                    secondsin << tempa[2];
                    times[0] = 0;
                    minutesin >> times[1];
                    secondsin >> times[2];
                }
                else if(numbers[2] < 100000)
                {
                    tempa[0] = temp.substr(0,1);
                    tempa[1] = temp.substr(1,2);
                    tempa[2] = temp.substr(3,2);
                    hoursin << tempa[0];
                    minutesin << tempa[1];
                    secondsin << tempa[2];
                    hoursin >> times[0];
                    minutesin >> times[1];
                    secondsin >> times[2];
                }
                else
                {
                    tempa[0] = temp.substr(0,2);
                    tempa[1] = temp.substr(2,2);
                    tempa[2] = temp.substr(4,2);
                    hoursin << tempa[0];
                    minutesin << tempa[1];
                    secondsin << tempa[2];
                    hoursin >> times[0];
                    minutesin >> times[1];
                    secondsin >> times[2];
                }

Not exactly elegant, I know, but it's something to work from and tighten up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.