Hi there, I have used the advice in this forum to solve most of the problems I have had so far but I am running out of time on this final assignment (due this evening) and I am stumped.

I need to write a function that takes a time in seconds and returns a formatted string (hh:mm:ss)

I am specifically not permitted to use an array (not that I know how to use one yet)

My code looks like this right now. I can easily obtain the components of the string in integer formatbut I cannot figure out how to pass them to the string with zero padding et al..

Help would be greatly appreciated.

#include <iostream>
#include <iomanip>

using namespace std;

int FormatTime(int ts, string formatted time);

int main ()
{
	int totalSeconds;
	string formattedTime;
	
	cout << "*********************************************************\n";
	cout << "*\tWelcome to the Time Re-format application\t*\n";
	cout << "*********************************************************\n\n\n";

	cout << "Please enter a time in seconds and press <Enter>: ";
	cin >> totalSeconds;
	
	FormatTime(totalSeconds, formattedTime);

	cout << "\n\nThe time in seconds you entered ("<< totalSeconds << " seconds) is equivelant to " << formattedTime;
	cout <<"\n\nThank you for using the time converter\n\n";

	return 0;
}
int FormatTime(int ts, string formattedTime)
{
	int hr, min, sec;

	hr = ts / 360;
	ts = ts % 360;
	min = ts / 60;
	sec = ts % 60;

	return (formattedTime);
}

I need to write a function that takes a time in seconds and returns a formatted string (hh:mm:ss)

Shouldn't it look more like this, then?

string FormatTime(int ts)
{
   string result;
   // ...
   return result;
}

And used like this?

string formattedTime = FormatTime(totalSeconds);

Shouldn't it look more like this, then?

string FormatTime(int ts)
{
   string result;
   // ...
   return result;
}

And used like this?

string formattedTime = FormatTime(totalSeconds);

Ok I have renamed the function but I still cannot see how to get the separate elements (hr, min, sec) into formattedTime with zero padding.

....

	string formattedTime = FormatTime(totalSeconds);

	cout << "\n\nThe time in seconds you entered ("<< totalSeconds << " seconds) is equivelant to " << formattedTime;
	cout <<"\n\nThank you for using the time converter\n\n";

	return 0;
}
string FormatTime(int ts)
{
	int hr, min, sec;
	string formattedTime

	hr = ts / 360;
	ts = ts % 360;
	min = ts / 60;
	sec = ts % 60;

// formattedTime = [I]created string here[/I]

	return (formattedTime);
}

I have taken the total seconds separated them, and now need to put it back together.

Sorry if I am still confused.

What methods are preferred/allowed to do the int to string conversion? What have you tried?

A simple zero pad test could be checking whether the value is less than ten and then adding a "0".

Hi,

I am unable to exactly understand your problem. If I understand correctly your problem is simply of formatting (zero padding). It would be good if you can show sample output. (I donn have C++ compiler right now). Are you allowed to use sprintf. It makes formatting real easy.

cheers,
aj.wh.ca

I have a user inputted number of seconds : 421

I need to output the string 01:01:01

I can break out the integer 421 into 1 hr 1 min and 1 sec no problem, but I cannot seem to get it together in the form hh:mm:ss

I would like to use sprintf but have no idea how to syntax it for my problem.

shr = sprintf(hr);

gives me an "no overloaded function takes 1 arguments" I don't know what that means other than that I am not using the command properly.

Thanks for all the help guys, I need it.

I would like to use sprintf but have no idea how to syntax it for my problem.

You use it with a char array -- which you are specifically not supposed to use, apparently.

What methods are preferred/allowed to do the int to string conversion? What have you tried?

Then I really don't get it.

I tried ToString, itoi, itol

These are commands that I got searching the web. none of these were taught in class. Hence my problem with this assignment.

If anyone can give me the command I should use and its syntax, I should be able to figure it out.

Thanks Dave! I got the formatting part. and it produces the format I rquire but I still do not have the output in the formattedTime string.

if I put anything infront of the << operator I get plenty of errors.

int hr, min, sec;
	string formattedTime;

	hr = ts / 360;
	ts = ts % 360;
	min = ts / 60;
	sec = ts % 60;

	//Formatted Time 
	 formattedTime<< setw(2) << setfill('0') << hr <<":" << setw(2) << setfill('0') << min <<":"<< setw(2) << setfill('0') << sec;

	return (formattedTime);

Thanks for all this, I really appreciate the help.

Thanks Dave! I got the formatting part. and it produces the format I rquire but I still do not have the output in the formattedTime string.

if I put anything infront of the << operator I get plenty of errors.

int hr, min, sec;
	string formattedTime;

	hr = ts / 360;
	ts = ts % 360;
	min = ts / 60;
	sec = ts % 60;

	//Formatted Time 
	 formattedTime<< setw(2) << setfill('0') << hr <<":" << setw(2) << setfill('0') << min <<":"<< setw(2) << setfill('0') << sec;

	return (formattedTime);

Thanks for all this, I really appreciate the help.

Hi,

You are not using the correct object. You can't use the << operator on a string. You can on the ostringstream object.

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

string FormatTime(int ts);

void main()
{
	int ts =480;
	string resultT;

	resultT = FormatTime(ts);
	cout << resultT << endl;
}
string FormatTime(int ts)
{
	ostringstream result;
	int hr, min, sec;

	hr = ts / 360;
	ts = ts % 360;
	min = ts / 60;
	sec = ts % 60;

	result << setw(2) << setfill('0') << hr  << ":"
          << setw(2) << setfill('0') << min << ":"
          << setw(2) << setfill('0') << sec;

	return result.str(); // .str() is a member function of ostringstream
the returns a string and stores it in result.
	
}

You need #include <sstream> to use an ostringstream object.

Cheers :cool:

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