?
Solved

Formatting Time

Posted on 2003-02-20
1
Medium Priority
?
194 Views
Last Modified: 2010-04-01
I am trying to write a function called FormatTime() that takes a single parmater that is a time in seconds and then return a string that represents the time in the following format: hh:mm:ss.

Below is what I have so far.  I am unsure of how to convert a int into a string and also to account for the space when there is only one digit in a location.  For example: 02:20:06.  

#include <iostream>
#include <string>
#include <complex>
#include <iomanip>
using namespace std;

int main(){

int InputSeconds;
cin>>InputSeconds;
int     hours = (InputSeconds/3600);
int minutes = ((InputSeconds%3600)/60);
int seconds = ((InputSeconds%3600)%60);
cout<<hours<<":"<<minutes<<":"<<seconds<<endl;

return 0;
}


//int FormatTime (int     hours, int minutes, int seconds)
//{
//int InputTimeTotal;
//cin>>InputTimeTotal;
//int     hours = (int seconds/3600);
//int minutes = ((InputTimeTotal%3600)/60);
//int seconds = ((InputTimeTotal%3600)%60);
//return hours;
//return minutes;
//return seconds;
//}





//int main(){
//int hh, mm, ss;
//int result;
//result= FormatTime(hh, mm, ss);
//cout<<FormatTime(hh)<<":"<<FormatTime
//(mm)<<":"<<FormatTime(ss)<<endl;

//return 0;
//}
0
Comment
Question by:ktran40
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
Salte earned 400 total points
ID: 7987755
Your best bet is to do cout.fill('0'); and cout.width(2); before you start outputting the numbers.

That will set minimum 2 digits and leading 0 if the value is less than 10.

cout.fill('0');
cout.width(2);
int hour = 13;
int min = 6;
int sec = 2;

// will print 13:06:02
cout << hour << ':' << min << ':' << sec;

If you want to do it really nice you save the return values from those functions. They are the old values:

char oldfill = cout.fill('0');
int oldw = cout.width(2);
cout << .... ;
cout.fill(oldfill);
cout.width(oldw);

This will restore the stream to the old values.

Alf
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question