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.

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by WaltP

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 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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.