Hello friends, I have a small problem with integers... I want to get the seconds of the time in format HHMMSS. My code is:

int get_seconds( int time ) {
	cout << time << endl;
	int sec = time % 100; time /= 100; cout << time << endl;
	sec += (time % 100)*60; time /= 100; cout << time << endl;
	return sec + (time*60)*60;
}

So... if the time=121314 the code works great, but if the time starts with zero then the time value is "converted into random number".

So, can you give me some code or example for solving this problem ?
Thanks.

How about giving an example of exactly what you want. Telling us it works 121314 doesn't tell us what is correct. Examples help.

When you start the int with a zero, I think it's getting interpreted as an octal number and then output as a decimal conversion of this. Try the code below:

#include <iostream>

using namespace std;

int main()
{
    int x1 = 010;   /* interpreted as an octal number */
    int x2 = 10;    /* just a regular decimal number  */
    cout << "x1 = " << x1 << " x2 = " << x2 << endl;
    return 0;
}

it should output x1 = 8 x2 = 10 . You can play about with the numbers to see what outputs you get. Try putting 9 as one of the digits, you should get a compilation error since this isn't an octal digit.

Edited 5 Years Ago by ravenous: n/a

When you start the int with a zero, I think it's getting interpreted as an octal number and then output as a decimal conversion of this.

But not if the value is input. Only if defined in the program.

This article has been dead for over six months. Start a new discussion instead.